无法为数组的一部分设置值
Unable To Set Value For Part Of An Array
在我的 WPF 应用程序中,在一个屏幕上,它有一系列文本框,如下所示:
此 window 的内容是从控件模板生成的,当按下按钮时,它会运行此方法将值更新为 class(thisPMX) 中的 public 对象如下代码所示):
private void UpdatePMX()
{
thisPMX.Name = tbName.Text;
thisPMX.Month = tbMonth.Text;
thisPMX.Day = tbDay.Text;
thisPMX.Year = tbYear.Text;
for(int i = 0; i < 8; i++)
{
string thisBehavior = "Behavior0" + (i + 1).ToString();
ControlTemplate temp = ((Label)this.FindName(thisBehavior)).Template;
if(((TextBox)temp.FindName("BName", behaviors[i])).Text.Length > 0)
{
thisPMX.Behaviors[i].Name = ((TextBox)temp.FindName("BName", behaviors[i])).Text;
}
if (((TextBox)temp.FindName("BehaviorWeight", behaviors[i])).Text.Length > 0)
{
thisPMX.Behaviors[i].Weight = ((TextBox)temp.FindName("BehaviorWeight", behaviors[i])).Text;
}
for(int e = 0; e < 10; e++)
{
if (((TextBox)temp.FindName(("S" + ((e + 1).ToString())), behaviors[i])).Text.Length > 0)
{
string scoreInst = ((TextBox)temp.FindName("S" + (e + 1).ToString(), behaviors[i])).Text;
thisPMX.Behaviors[i].SAll[e] = (scoreInst.ToString().Trim());
string Inst = thisPMX.Behaviors[i].SAll[e]; // Break Here For Testing Value
}
}
}
}
此方法按预期工作,直到最后一个 for 语句未更新可能分数框中的值。在这一点上,我在它周围添加了字符串以进行调试,如上所示。当我在评论指出的点放置断点时,scoreInst 的值为“1”,i 为 0,e 为 0,但是当我将 scoreInst 分配给 thisPMX.Behaviors[i].SAll[e]
时,它似乎不接受该值和 Inst结果为空。
这里是 class 的行为,其中包含 SAll 字符串[]:
public class Behavior
{
public Behavior()
{
}
public string Name { get; set; }
public string BehaviorMeasure { get; set; }
public string Weight { get; set; }
public string S1 { get; set; }
public string S2 { get; set; }
public string S3 { get; set; }
public string S4 { get; set; }
public string S5 { get; set; }
public string S6 { get; set; }
public string S7 { get; set; }
public string S8 { get; set; }
public string S9 { get; set; }
public string S10 { get; set; }
public bool ValuesSatisfied
{
get
{
if ((ActiveScores < 3) || !(BehaviorMeasure.Length == 0) || !(Weight.Length == 0) || !(Name.Length == 0))
{
return false;
}
else
{
return true;
}
}
}
public bool HasActiveValues
{
get
{
if (Name != null && Weight != null)
{
if ((ActiveScores > 0) || (Name.Length > 0) || (Weight.Length > 0))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
public string[] SAll
{
get
{
string[] Scores = { S1, S2, S3, S4, S5, S6, S7, S8, S9, S10 };
return Scores;
}
}
public int ActiveScores
{
get
{
int count = 0;
for(int i = 0; i < SAll.Length; i++)
{
if (SAll[i] != null && SAll[i].Length > 0)
{
++count;
}
}
return count;
}
}
}
我没有收到任何异常或错误,但当调用 UpdatePMX() 方法时,可能分数文本框的值未设置为 SAll 数组值。
期望的结果是此方法能够成功地将 xaml 中各种文本框的值传递到 "thisPMX" class 实例中的各种行为值.
备注:
thisPMX 是一个 public class 实例,其中有 8 个行为实例 class 如上所示。
如果需要更多信息,请告诉我,否则感谢您的帮助。
问题是您在 SAll
中设置的值总是丢失,因为 SAll
总是返回一个新数组。因此,当您尝试从刚刚插入的数组中获取数据时,您实际上只是在获取一个新数组。
问题代码:
public string[] SAll
{
get
{
string[] Scores = { S1, S2, S3, S4, S5, S6, S7, S8, S9, S10 };
return Scores;
}
}
应该更像是:
private string[] _sAll;
public string[] SAll
{
get
{
if(_sAll == null)
_sAll = { S1, S2, S3, S4, S5, S6, S7, S8, S9, S10 };
return _sAll;
}
}
但是,数组中的任何更改都不会反映回 S1
、S2
等。因此,最好将它们更改为更像:
public String S1
{
get{ return SAll[0];}
set{ SAll[0] = value;}
}
在我的 WPF 应用程序中,在一个屏幕上,它有一系列文本框,如下所示:
此 window 的内容是从控件模板生成的,当按下按钮时,它会运行此方法将值更新为 class(thisPMX) 中的 public 对象如下代码所示):
private void UpdatePMX()
{
thisPMX.Name = tbName.Text;
thisPMX.Month = tbMonth.Text;
thisPMX.Day = tbDay.Text;
thisPMX.Year = tbYear.Text;
for(int i = 0; i < 8; i++)
{
string thisBehavior = "Behavior0" + (i + 1).ToString();
ControlTemplate temp = ((Label)this.FindName(thisBehavior)).Template;
if(((TextBox)temp.FindName("BName", behaviors[i])).Text.Length > 0)
{
thisPMX.Behaviors[i].Name = ((TextBox)temp.FindName("BName", behaviors[i])).Text;
}
if (((TextBox)temp.FindName("BehaviorWeight", behaviors[i])).Text.Length > 0)
{
thisPMX.Behaviors[i].Weight = ((TextBox)temp.FindName("BehaviorWeight", behaviors[i])).Text;
}
for(int e = 0; e < 10; e++)
{
if (((TextBox)temp.FindName(("S" + ((e + 1).ToString())), behaviors[i])).Text.Length > 0)
{
string scoreInst = ((TextBox)temp.FindName("S" + (e + 1).ToString(), behaviors[i])).Text;
thisPMX.Behaviors[i].SAll[e] = (scoreInst.ToString().Trim());
string Inst = thisPMX.Behaviors[i].SAll[e]; // Break Here For Testing Value
}
}
}
}
此方法按预期工作,直到最后一个 for 语句未更新可能分数框中的值。在这一点上,我在它周围添加了字符串以进行调试,如上所示。当我在评论指出的点放置断点时,scoreInst 的值为“1”,i 为 0,e 为 0,但是当我将 scoreInst 分配给 thisPMX.Behaviors[i].SAll[e]
时,它似乎不接受该值和 Inst结果为空。
这里是 class 的行为,其中包含 SAll 字符串[]:
public class Behavior
{
public Behavior()
{
}
public string Name { get; set; }
public string BehaviorMeasure { get; set; }
public string Weight { get; set; }
public string S1 { get; set; }
public string S2 { get; set; }
public string S3 { get; set; }
public string S4 { get; set; }
public string S5 { get; set; }
public string S6 { get; set; }
public string S7 { get; set; }
public string S8 { get; set; }
public string S9 { get; set; }
public string S10 { get; set; }
public bool ValuesSatisfied
{
get
{
if ((ActiveScores < 3) || !(BehaviorMeasure.Length == 0) || !(Weight.Length == 0) || !(Name.Length == 0))
{
return false;
}
else
{
return true;
}
}
}
public bool HasActiveValues
{
get
{
if (Name != null && Weight != null)
{
if ((ActiveScores > 0) || (Name.Length > 0) || (Weight.Length > 0))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
public string[] SAll
{
get
{
string[] Scores = { S1, S2, S3, S4, S5, S6, S7, S8, S9, S10 };
return Scores;
}
}
public int ActiveScores
{
get
{
int count = 0;
for(int i = 0; i < SAll.Length; i++)
{
if (SAll[i] != null && SAll[i].Length > 0)
{
++count;
}
}
return count;
}
}
}
我没有收到任何异常或错误,但当调用 UpdatePMX() 方法时,可能分数文本框的值未设置为 SAll 数组值。
期望的结果是此方法能够成功地将 xaml 中各种文本框的值传递到 "thisPMX" class 实例中的各种行为值.
备注:
thisPMX 是一个 public class 实例,其中有 8 个行为实例 class 如上所示。
如果需要更多信息,请告诉我,否则感谢您的帮助。
问题是您在 SAll
中设置的值总是丢失,因为 SAll
总是返回一个新数组。因此,当您尝试从刚刚插入的数组中获取数据时,您实际上只是在获取一个新数组。
问题代码:
public string[] SAll
{
get
{
string[] Scores = { S1, S2, S3, S4, S5, S6, S7, S8, S9, S10 };
return Scores;
}
}
应该更像是:
private string[] _sAll;
public string[] SAll
{
get
{
if(_sAll == null)
_sAll = { S1, S2, S3, S4, S5, S6, S7, S8, S9, S10 };
return _sAll;
}
}
但是,数组中的任何更改都不会反映回 S1
、S2
等。因此,最好将它们更改为更像:
public String S1
{
get{ return SAll[0];}
set{ SAll[0] = value;}
}