Saturday, May 9, 2009

I have a problem with my C# code. Could anyone offer a little wisdom as to how I can fix it.?

int upc1, upc2, upc3, upc4, upc5, upc6, upc7, upc8;





DateTime reorderDate;


string code = txtUpc.Text;





try


{


upc2 = int.Parse(code.Substring(0, 1));


upc3 = int.Parse(code.Substring(1, 1));


upc4 = int.Parse(code.Substring(2, 1));


upc5 = int.Parse(code.Substring(3, 1));


upc6 = int.Parse(code.Substring(4, 1));


upc7 = int.Parse(code.Substring(5, 1));


upc8 = int.Parse(code.Substring(7, 1));


}


catch


{


MessageBox.Show("UPC must be in the format mmmppp-c.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);


txtUpc.Focus();


return;


}





bool upcLength = ((code.Length == 8) %26amp;%26amp; (code.Substring(6, 1) == "-"));








if (!upcLength)


{


MessageBox.Show("UPC must be in the format mmmppp-c.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);


txtUpc.Focus();


}


{


}





int num1 = (upc2 + upc4 + upc6) * 5;


int num2 = upc3 + upc5 + upc7;


int num3 = num1 = num2;


int num4 = 10 - (num3 % 10);





if (num4 != 10)


{


MessageBox.Show("UPC must be in the format mmmppp-c.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);


txtUpc.Focus();


}


else


{


}





if (num4 == upc8)


{


txtDescription.Focus();


}


else


{


}

I have a problem with my C# code. Could anyone offer a little wisdom as to how I can fix it.?
int num3 = num1 = num2;





That line is the culprit. Just before that, you assigned two different values to num1 and num2. Then why are you assigning num1 the same value as num2 now? Looks funny. Hence, there is your problem. To fix this, you need to find out what's the real algorithm. May be





int num3 = num1 - num2





??





or





int num3 = num1 + num2





??





something like that?
Reply:Pretty sure your problem lies in these 3 lines somewhere.


int num3 = num1 = num2;


int num4 = 10 - (num3 % 10);


if (num4 != 10)





the firstline I don't understand why you did some calculation for num1 and then assigned num2 to num3 and num1...you just lost the result from the first calculation and num1, num2, and num3 now all hold the same value.





I would make sure that what is stored in num3 can be evenly divided by 10 (10,20,30, 340, etc)

sundew

No comments:

Post a Comment