在C#中的For循环中发现问题 [英] Problem Found in For Loop in C#
问题描述
Hello All
实际上我没有遇到问题。我试图从数据库传递值作为上一个值(PreValue)和从文本框字段(CurValue)和FieldValue执行的当前值从数据库列名称。我将比较如果PreValue不等于CurValue,那么记录将保存到表中。我也没有获得PreValue数组格式。我对这段代码很困惑。实际上我想检查值,如果任何更改CurVale与PreVale然后循环将工作否则。任何人帮助我我的不匹配。
<前lang =c#> 字符串 [] PreValue = < span class =code-keyword> new string [] {Program.GetCharacterValue( 从SCVBAE中选择FaName,MoName,Address,HasAE,Misuse,其中FormSL =' + txtFrmSerial.Text.Trim()+ 'AND PID =' + txtPID.Text.Trim()+ ')};
string [] FieldValue = new string [ 54 ] { FaName, MoName, 地址, HasAE, 滥用};
string [] CurValue = new string [ 54 ] {txtFaName.Text,txtMoName.Text,txtAddress.Text,txtHasAE.Text,txtMisuse.Text};
尝试
{
AEProperty Add = new AEProperty();
Add.FormSl = txtFrmSerial.Text.Trim();
Add.PID = txtPID.Text;
for ( int i = 0 ; i < 6 ; i ++)
{
if (PreValue [i] .ToString()!= CurValue [i] .ToString())
{
Add.PreValue = PreValue [I]的ToString();
Add.CurValue = CurValue [i] .ToString();
Add.FieldName = FieldValue [i] .ToString();
Add.FileNo = 1;
Add.ReasonADT = 打孔错误;
Add.UpdateBy = Program.EnterBy;
Add.UpdateDt = DateTime.Today.ToString( MM / dd / yyyy) ;
Add.UpdateTm = DateTime.Now.ToString( HH:mm:ss tt );
DSDBHandler AddHandler = new DSDBHandler();
AddHandler.AddADT(Add);
}
}
}
catch (例外情况)
{
MessageBox.Show ( 记录无法插入!正确检查 + ex.Message);
txtFrmSerial.Focus();
}
在循环中,看看这一行:Add.FieldName = FieldValue [i] .ToString();
FieldValue [i]
可以有一个最大值i
5 ,因为那是仍然符合循环条件的最大整数i< 6
。
但是 FieldValue [5] 不存在。 FieldValue 有5名成员。数组在C#中是零基础,因此范围从 0 到 4 。
如何解决我不知道,因为我不知道该代码应该做什么。数组FieldValue
和CurValue
都被54个项目声明,但只有5个被定义。 br $> b $ b
我刚用VS2008查看过。 FieldValue 和 CurValue 的声明甚至无法编译。
删除两次出现的54。然后,使用其中一个作为循环边界:string [] FieldValue = new string [] { FaName, MoName, < span class =code-string> Address, HasAE, 误用};
string [] CurValue = new string [] {txtFaName.Text,txtMoName.Text,txtAddress.Text,txtHasAE.Text,txtMisuse.Text};
// [..]
for ( int i = 0 ; i < FieldValue.Length; i ++)
{
[/ Edit]
索引将从0到53,因为每个数组中有54个项目。
更改循环:
for(int i = 0; i< 55; i ++)
to
for(int i = 0; i< 54; i ++)
否则它将捕获超出约束的异常。
Hello All
Actually I am not getting problem. I am trying to pass value from database as Previous Value(PreValue) and Current value carrying out from text box field(CurValue) and FieldValue is taking from database column name. I will just compare if PreValue not equal to CurValue then the record will be saved into table. I am not getting PreValue array format also. I am confused about that code. Actually I want to check value if any changes CurVale with PreVale then Loop will work else not. Anybody help me my mismatch.
string[] PreValue = new string[] { Program.GetCharacterValue("Select FaName,MoName,Address,HasAE,Misuse from SCVBAE where FormSL='" + txtFrmSerial.Text.Trim() + "' AND PID='" + txtPID.Text.Trim() + "'") };
string[] FieldValue = new string[54] { "FaName","MoName","Address","HasAE","Misuse" };
string[] CurValue = new string[54] { txtFaName.Text, txtMoName.Text, txtAddress.Text, txtHasAE.Text, txtMisuse.Text };
try
{
AEProperty Add = new AEProperty();
Add.FormSl = txtFrmSerial.Text.Trim();
Add.PID = txtPID.Text;
for (int i = 0; i < 6; i++)
{
if (PreValue[i].ToString() != CurValue[i].ToString())
{
Add.PreValue = PreValue[i].ToString();
Add.CurValue = CurValue[i].ToString();
Add.FieldName = FieldValue[i].ToString();
Add.FileNo = "1";
Add.ReasonADT = "Punching error";
Add.UpdateBy = Program.EnterBy;
Add.UpdateDt = DateTime.Today.ToString("MM/dd/yyyy");
Add.UpdateTm = DateTime.Now.ToString("HH:mm:ss tt");
DSDBHandler AddHandler = new DSDBHandler();
AddHandler.AddADT(Add);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Record cannot insert ! check properly" + ex.Message);
txtFrmSerial.Focus();
}
Within the loop, look at this line:Add.FieldName = FieldValue[i].ToString();
FieldValue[i]
can have a maximumi
of 5, since that's the largest integer that still conforms the loop conditioni < 6
.
But FieldValue[5] doesn't exist. FieldValue has 5 members. Arrays are "zero-based" in C# and therefore range from 0 to 4.
How to solve that I have no idea, since I don't know what that code should do. I'm baffled by the arraysFieldValue
andCurValue
being both declared with 54 items, but having only 5 defined.
[Edit]
I just checked with VS2008. The declarations of FieldValue and CurValue don't even compile.
Drop both occurances of "54". Then, use one of them as loop boundary:string[] FieldValue = new string[] { "FaName","MoName","Address","HasAE","Misuse" }; string[] CurValue = new string[] { txtFaName.Text, txtMoName.Text, txtAddress.Text, txtHasAE.Text, txtMisuse.Text }; // [..] for (int i = 0; i < FieldValue.Length; i++) {
[/Edit]
Index will go from 0 to 53 as you are having 54 items in each array.
change your loop from :
for (int i = 0; i < 55; i++)
to
for (int i = 0; i < 54; i++)
else it will catch out of bound exception.
这篇关于在C#中的For循环中发现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!