Results 1 to 7 of 7

Thread: [RESOLVED] for loop question

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: for loop question

    This:
    VB Code:
    1. For i As Integer = 0 To myTable.Rows.Count - 1 Step 1
    2.    '...
    3. 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:
    1. For i As Integer = myTable.Rows.Count - 1 To 0 Step -1
    2.    '...
    3. Next i
    yourself, but in case you can't it's:
    Code:
    for (int i = myTable.Rows.Count - 1; i >= 0; i--)
    {
        // ...
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: for loop question

    so this is the correct syntax

    for(starting count; condition to stop; howmuch to step by)

    ?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    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#.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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
  •  



Click Here to Expand Forum to Full Width