Link to home
Start Free TrialLog in
Avatar of Mike_Stevens
Mike_StevensFlag for United States of America

asked on

Problem using Select Case with nested loops using vb.net

I am working on a project that requires some looping predetermined number of times.  Within the For Next Loop I am trying to use Select Case to read string values until a certain condition is met.  Below is my code and hopefully an understandable description of what my problem is:

To simply the example I will explain what happens within the do while loop...... A text file is broken down line by line and then each line is broken into “segments” using the split function.  Using Select Case I am reading the first “Segment” of line to determine if a specific value is contained in the “segment”

xx = 10  Value of xx is determine earlier in the code

For k = 1 To xx

          xDone = False

               Do While xDone = False

                    Select Case xSeg(0)
                          Case Is = “ABC”
                                ‘Then do something and continue on
                          Case Is = “DEF”
                                ‘Then do something and continue on
                         Case is = “XYZ”
                                      xDone = ‘True’  Indicate condition was met
                                     'Condition met...move on
                         End Select

                       Code --- > ‘If xDone is still false then move to next line of data and break into segments and start process again,

                Loop

               When the ‘xDone’ condition has been met and xDone = True the Do While Loop should exit back into the for next loop at the .    

Next k

The problem I am having is that this process should run ten times as indicated in the for (For k = 1 To xx ) next but it only will run a maximum of three times.    Sometimes the data that is being process requires that the For Next should only loop once or twice or sometimes more but it will only loop a maximum of three times.  

It has something to do with the Select Case because if I remove that whole section of the code everything works fine.  

Im not a vb.net expert and I cant figure it out on my own.  Any help would be appreciated.
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

Sounds like you are modifying the value of k within the for next block as this is the only reason that it would not be iterated 10 times.

You could put a debug line at the top and bottom of the for loop to print the current value of k

For k = 1 To xx

  console.writeline(k.tostring)

.......

  console.writeline(k.tostring)
Next k

Open in new window


this will tell you if the value is being modified during the execution of the loop
Avatar of Mike_Stevens

ASKER

The value of K is being modified as it should and gets to three and that's when for next look gets exited.  It should go to 10
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The value of K is being modified by what, the for next will modify (increment it anyway). As James has said there are better tools to exit a loop than by modifying the loop counter within the loop.

What is the code that is modifying the value of k?