我有一个名为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 get "firstName" value as null why????
}
}
class Program
{
static void Main(string[] args)
{
var obj = new Data();
obj.getdata();
obj.display();
Console.ReadLine();
}
}
为什么在拨打disp()
时我会获得空值,另外我只想访问firstName
中lastName
和GetDataToXml()
的值,即使getData()
paper.lines.get(this-1);
1}}函数被调用。尽管我将它赋值为public,但这个变量的范围是什么?
答案 0 :(得分:1)
为了帮助你,我重新设计了你的例子:
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;
}
}
在GetDataToXML方法中放置一个断点并检查FirstName和LastName值。