c# - public string's value is always null -
i started learning c# few days ago , i'm having problem public strings, i'm trying write program copies , replaces files practice i'm having problem public strings no matter how try change code couldn't figure out myself came here help
what doing wrong?
here's code:
namespace extractor { public partial class form1 : form { public string s { get; set; } public string sselectedpath { get; set; } public string beckup { get; set; } public form1() { initializecomponent(); } private void direc_click(object sender, eventargs e) { folderbrowserdialog fbd = new folderbrowserdialog(); fbd.description = "select folder"; if (fbd.showdialog() == dialogresult.ok) { string sselectedpath = fbd.selectedpath; } } private void choof_click(object sender, eventargs e) { openfiledialog choofdlog = new openfiledialog(); choofdlog.filter = "all files (*.*)|*.*"; choofdlog.filterindex = 1; choofdlog.multiselect = true; if (choofdlog.showdialog() == dialogresult.ok) { string s = choofdlog.filename; } } private void button3_click(object sender, eventargs e) { replacefile( s, sselectedpath, beckup); } public static void replacefile(string filetomoveanddelete, string filetoreplace, string backupoffiletoreplace) { file.replace(filetomoveanddelete, filetoreplace, backupoffiletoreplace, false); } private void button1_click(object sender, eventargs e) { folderbrowserdialog fbb = new folderbrowserdialog(); fbb.description = "select folder"; if (fbb.showdialog() == dialogresult.ok) { string beckup = fbb.selectedpath; } } }
your mistake here :
if (choofdlog.showdialog() == dialogresult.ok) { string sselectedpath = choofdlog.filename; }
you're using local variable, not member variable. so, write :
if (choofdlog.showdialog() == dialogresult.ok) { sselectedpath = choofdlog.filename; }
or, better, if doesn't want make mistake, use this.
when write member variable or method :
if (choofdlog.showdialog() == dialogresult.ok) { this.sselectedpath = choofdlog.filename; }
Comments
Post a Comment