C# 一个变量之后怎么可能被覆盖?
我在读取现有XML文件时遇到了一个奇怪的问题。我有一个以步骤为元素的XML文件。如果XML中有两个步骤,我希望在列表框中显示它们 我用XmlDoc构建了一个列表:C# 一个变量之后怎么可能被覆盖?,c#,xml,reference,C#,Xml,Reference,我在读取现有XML文件时遇到了一个奇怪的问题。我有一个以步骤为元素的XML文件。如果XML中有两个步骤,我希望在列表框中显示它们 我用XmlDoc构建了一个列表: var myStepList = new Steps(xmlDoc); 然后我计算步骤,如果里面有两个步骤,则转到案例2 switch (myStepList.amountOfSteps) { case 1: var StepData = myStepList.
var myStepList = new Steps(xmlDoc);
然后我计算步骤,如果里面有两个步骤,则转到案例2
switch (myStepList.amountOfSteps)
{
case 1:
var StepData = myStepList.GetStepData(myStepList.stepList[0]);
ListBoxSteps.Items.Add(StepData[0].ToString());
TextBoxStepNo.Text = StepData[1].ToString();
TextBoxStepColorCode.Text = StepData[2].ToString();
break;
case 2:
var StepData1 = myStepList.GetStepData(myStepList.stepList[0]);
var StepData2 = myStepList.GetStepData(myStepList.stepList[1]);
ListBoxSteps.Items.Add(StepData1[0].ToString());
ListBoxSteps.Items.Add(StepData2[0].ToString());
TextBoxStepNo.Text = StepData1[1].ToString();
TextBoxStepNo2.Text = StepData2[1].ToString();
TextBoxStepColorCode.Text = StepData1[2].ToString();
TextBoxStepColorCode2.Text = StepData2[2].ToString();
break;
}
当使用正确的内容定义Stepdata2时,它将覆盖StepData1。我不知道为什么,因为在它有正确的内容之前
以下是我的课堂教学步骤:
class Steps
{
private List<XElement> StepList;
private readonly XAttribute[] StepData;
private int AmountOfSteps = 0;
private readonly int AmountOfStepAttributes = 3;
public Steps(XmlDoc xmlDoc)
{
StepList = xmlDoc.GetStepList();
StepData = new XAttribute[AmountOfStepAttributes];
}
public XAttribute[] GetStepData(XElement step)
{
StepData[0] = step.Attribute("name");
StepData[1] = step.Attribute("stepNo");
StepData[2] = step.Attribute("colorCode");
return StepData;
}
public List<XElement> stepList
{
get { return StepList; }
}
public int amountOfSteps
{
get
{
AmountOfSteps = stepList.Count();
return AmountOfSteps;
}
}
public int amountStepAttributes
{
get { return AmountOfStepAttributes; }
}
这是我的类,名为XmlDoc:
class XmlDoc
{
private List<string> ListFileNames;
private XDocument XDoc;
private XAttribute RootAttribute;
private List<XElement> StepList;
public XmlDoc()
{
StepList = new List<XElement>();
ListFileNames = new List<string>();
}
public List<string> GetXmlFilesList(string path)
{
List<string> ListFileNames = new List<string>(Directory.GetFiles(path));
return ListFileNames;
}
public XDocument GetXDoc(string selectedXmlFile)
{
XDoc = XDocument.Load(selectedXmlFile);
return XDoc;
}
public XAttribute GetRootAttribute()
{
RootAttribute = XDoc.Root.Attribute("No");
return RootAttribute;
}
public List<XElement> GetStepList()
{
StepList = XDoc.Root.Elements("Step").ToList();
return StepList;
}
}
您在类中使用实例字段,而局部变量就可以了。我将分步骤进行两个修复,我将在XmlDoc和其他类中解决类似问题留给您来做:
class Steps
{
private List<XElement> StepList;
//Remove this, only needed inside GetStepData
//private readonly XAttribute[] StepData;
//Remove this, not needed
//private int AmountOfSteps = 0;
private readonly int AmountOfStepAttributes = 3;
public Steps(XmlDoc xmlDoc)
{
StepList = xmlDoc.GetStepList();
}
public XAttribute[] GetStepData(XElement step)
{
//Create a new array here
var StepData = new XAttribute[AmountOfStepAttributes];
StepData[0] = step.Attribute("name");
StepData[1] = step.Attribute("stepNo");
StepData[2] = step.Attribute("colorCode");
return StepData;
}
public List<XElement> stepList
{
get { return StepList; }
}
public int amountOfSteps
{
get
{
//Just directly return the count
return stepList.Count();
}
}
public int amountStepAttributes
{
get { return AmountOfStepAttributes; }
}
上面的评论应该全部删除——它们只是描述了什么发生了变化以及为什么发生了变化
在当前代码中,步骤中包含一个数组,每次调用GetStepData时都会覆盖该数组。在我上面的固定代码中,每次调用一个新数组时,我们都会分配一个新数组,并且只在方法调用结束之前操作该数组
一个不明显的提示-打开选项,将警告视为项目的错误。简单代码不应该生成警告,它应该在XmlDocs类中至少标记一个问题,即实例成员ListFileName已初始化但从未使用
Ob link Eric Lippert的你是说stepdata1的da包含StepData2的内容?你介意问我为什么要使用反向大小写来命名私有变量和属性吗?为什么StepData是Steps中的一个实例字段?这就是共享发生的地方,但是知道是什么导致了这样的选择是很有用的。您可以在您的问题中包含XML文件吗?“我们需要一本书。@chetan:是的,它们的内容都一样。”。我不知道你说的反向是什么意思casing@Damien:这个选择是错误的+您可以在var StepData=new XAttribute[amountofsteptattributes];之后使用具有索引的对象初始值设定项;。谢谢你,达米恩!