What is the scope of public variable in different methods of a same class in Console C# -


i have class named data used retrieve user's data

    class data     {         public string firstname;         public string lastname;         public void getdata()         {             firstname = "abc";             lastname = "xyz";         }         public static xdocument getdatatoxml()         {             var objget = new data();             objget.getdata();             xdocument doc = new xdocument(                new xelement("firstname ", objget.firstname),                new xelement("lastname", objget.firstname));             return doc;         }         public void display()         {             string fdata = firstname;  //i "firstname"  value null why????         }     }     class program     {         static void main(string[] args)         {             var obj = new data();             obj.getdata();             obj.display();             console.readline();         }     } 

why null value when call disp() ,also want access values of firstname , lastname in getdatatoxml() there getdata() function gets called.what scope of variable in spite me assigning public?

to little have redesigned example :

class program {     static void main(string[] args)     {         var obj = new data();         obj.setdata("first", "last");         obj.getdatatoxml();         console.readline();     } }  class data {     public string firstname { get; set; }     public string lastname { get; set; }      public void setdata(string firstname, string lastname)     {         firstname = firstname;         lastname = lastname;     }     public xdocument getdatatoxml()     {         xdocument doc = new xdocument(            new xelement("firstname ", firstname),            new xelement("lastname", lastname));         return doc;     } } 

put breakpoint inside getdatatoxml method , check both firstname , lastname values.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -