Answered by:
Comparing array elements???

Question
-
I'm trying to compare two elements in an array.. something like this..
if (row1[3] > row2[4])
{
letter = 'a';
}
is this possable or can you please show me a better way..
ThanksMonday, August 10, 2009 2:49 PM
Answers
-
pretty simple from a textbox numbers get put into an array if one element in the array is greater than the other then it put the letter in "char letter;
int[] row1 = new int[5];
int[] row2 = new int[6];
char letter;if (row1[3] > row2[4])
{
letter = 'a';
}
The code you posted is alright, but you have not showed the full function. The code snippet needs to be inside a method (cannot be loosely inserted inside the class body directly).
http://blog.voidnish.com- Marked as answer by Harry Zhu Monday, August 17, 2009 8:19 AM
Monday, August 10, 2009 3:25 PMModerator
All replies
-
Although you state that you are comparing two elements in an array, your code actually uses 2 different variables - row1 and row2. Unless they both refer to the same array object, you are comparing two items from 2 different arrays (and using non-matching indices).
http://blog.voidnish.com- Edited by Nishant SivakumarModerator Monday, August 10, 2009 2:54 PM typo
Monday, August 10, 2009 2:54 PMModerator -
Yes, that will work. Was there a particular aspect of this comparison that you were hoping would be better in some way?Monday, August 10, 2009 2:55 PM
-
I keep getting this error .. Invaild token 'if ' in class ..??Monday, August 10, 2009 3:05 PM
-
I keep getting this error .. Invaild token 'if ' in class ..??
Post the entire function here. I am sure there's some syntactic error.
http://blog.voidnish.comMonday, August 10, 2009 3:08 PMModerator -
pretty simple from a textbox numbers get put into an array if one element in the array is greater than the other then it put the letter in "char letter;
int[] row1 = new int[5];
int[] row2 = new int[6];
char letter;if (row1[3] > row2[4])
{
letter = 'a';
}Monday, August 10, 2009 3:21 PM -
Thanks for the help .. fig out I put the code in the wrong location....LOL
LaterMonday, August 10, 2009 3:25 PM -
pretty simple from a textbox numbers get put into an array if one element in the array is greater than the other then it put the letter in "char letter;
int[] row1 = new int[5];
int[] row2 = new int[6];
char letter;if (row1[3] > row2[4])
{
letter = 'a';
}
The code you posted is alright, but you have not showed the full function. The code snippet needs to be inside a method (cannot be loosely inserted inside the class body directly).
http://blog.voidnish.com- Marked as answer by Harry Zhu Monday, August 17, 2009 8:19 AM
Monday, August 10, 2009 3:25 PMModerator