|
-
Apr 15th, 2006, 04:56 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [1.0/1.1] Loop problem?
I only started C# today (i already know VBNET), and can't work out whats wrong with this loop:
Code:
static void Main(string[] args)
{
int[] i = new int[3];
i[0] = 2;
i[1] = 4;
i[2] = 61;
int sum;
for (int x = 0; x < 2; x++)
{
sum += i[x];
}
Console.WriteLine(sum);
Console.ReadLine();
}
Sorry for such a simple question.
-
Apr 15th, 2006, 05:15 PM
#2
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Loop problem?
Wait....i know. x doesn't have a value yet, so it can't be used.
-
Apr 15th, 2006, 06:47 PM
#3
Re: [RESOLVED] [1.0/1.1] Loop problem?
It's not 'x' that's the issue. It's 'sum'. You need to initialise all variables in C#.This is one of the ways in which C# is stricter than VB.NET. If you want 'sum' to be zero then you have to specify it. That way you actually have to think about what value you want a variable to have and you won't simply forget to set a value and have a variable default to zero when that isn't what you want.
-
Apr 15th, 2006, 07:43 PM
#4
Thread Starter
Hyperactive Member
Re: [RESOLVED] [1.0/1.1] Loop problem?
Thats what i meant .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|