Opening text files in C# application -


i'm still beginner when comes programming , small application did following c# tutorial.

private void viewimagestoolstripmenuitem_click(object sender, eventargs e)      {  string openedfile = "";          openfd.title = "insert text file";         openfd.initialdirectory = "c:";         openfd.filename = "";         openfd.filter = "text files|*.txt|word documents|*.doc|allfiles|*.*";           if (openfd.showdialog() == dialogresult.cancel)          {             messagebox.show("operation canceled");         }          if (openfd.showdialog() != dialogresult.cancel)          {             openedfile = openfd.filename;             richtextbox1.loadfile(openedfile,richtextboxstreamtype.plaintext);         } 

while doing noticed if change same application's code order 2 lines-

   string openedfile = "";    openedfile = openfd.filename;  

like below throw me error when debugging - empty path name not legal.

    private void opentoolstripmenuitem_click(object sender, eventargs e)     {           openfd.title = "insert text file";         openfd.initialdirectory = "c:";         openfd.filename = "";         openfd.filter = "text files|*.txt|word documents|*.doc|allfiles|*.*";          **string openedfile = "";         openedfile = openfd.filename;**           if (openfd.showdialog() == dialogresult.cancel)          {             messagebox.show("operation canceled");         }          if (openfd.showdialog() != dialogresult.cancel)          {              richtextbox1.loadfile(openedfile,richtextboxstreamtype.plaintext);         } 

isn't there way understand errors in these type situations. specific order of coding application this?

well, idea simple cannot use variable has been not initialized. in case sm thing same happening. in first code openedfile = openfd.filename; being executed after dialouge has been shown. file name comes correctly. in second openedfile = openfd.filename; getting initialized before dilogue has been shown. since there no dialogue name null, hence gives error.

note. have used initialized word not in tecnical manner.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -