|
-
Sep 26th, 2006, 07:37 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] for loop question
im having trouble with the santax of the for loop in C#
in vb.net it would be something like
For index = 0 To table.Columns.Count - 1 Step 1
some stuff
Next
in c#
im getting stuck
/code
int index = table.columns.count -1;
For index ( how do i tell it to step down by -1 till 0)
{
some stuff
}
Last edited by Crash893; Sep 26th, 2006 at 07:53 PM.
-
Sep 26th, 2006, 08:09 PM
#2
Re: for loop question
Code:
for (index = 0 ; index < table.Columns.Count ; index++){
SomeStuff;
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 26th, 2006, 08:18 PM
#3
Re: for loop question
This:
VB Code:
For i As Integer = 0 To myTable.Rows.Count - 1 Step 1
'...
Next i
equals this:
Code:
for (int i = 0; i <= myTable.Rows.Count - 1; i++)
{
// ...
}
although you'd normally exclude the (- 1) and use less than instead of greater than. Knowing that, you should be able to convert this:
VB Code:
For i As Integer = myTable.Rows.Count - 1 To 0 Step -1
'...
Next i
yourself, but in case you can't it's:
Code:
for (int i = myTable.Rows.Count - 1; i >= 0; i--)
{
// ...
}
-
Sep 26th, 2006, 10:56 PM
#4
Thread Starter
Fanatic Member
Re: for loop question
so this is the correct syntax
for(starting count; condition to stop; howmuch to step by)
?
-
Sep 26th, 2006, 11:59 PM
#5
Re: for loop question
No. the syntax is:
Code:
for (initial action; condition to continue; action to take after each iteration)
This is a valid 'for' loop:
Code:
DateTime endTime = DateTime.Now.AddSeconds(20);
for (MessageBox.Show("Begin loop");
DateTime.Now < endTime;
MessageBox.Show("End iteration"))
{
}
There's nothing inherent in the 'for' statement that requires a loop counter at all. Have you read the MSDN help topic for the C# 'for' statement? If not then why not?
-
Sep 27th, 2006, 08:21 AM
#6
Re: for loop question
One thing you should be aware of (Crash893) is that in VB, the expression "table.Columns.Count - 1" gets evaluated exactly once.
In C#, the "condition to continue" gets evaluated on every iteration. In you example, it probably doesn't make a difference, but this is a common source of errors when people convert these loops from VB to C#.
-
Sep 27th, 2006, 06:34 PM
#7
Thread Starter
Fanatic Member
Re: for loop question
thanks for the info all
i am in the proccess of trying it now so ill let you know
thanks
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
|