How do I write a loop in Visual Basic?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
in c# i can write this lovely thing:

VB.NET:
String s;
for(int linesRead=0, s=fileReader.ReadLine(); s!=null; linesRead+=1, s=fileReader.ReadLine() ){
  if(s.length != 100){
    MessageBox.Show("Line is not right length - ignoring!")
    continue;
  }
 
  if(s.startsWith("HEADER"))
    continue; //ignore header lines
 
  if(some other test){
    MessageBox.show("some other test failed too, progressing to next line");
    continue;
  }
 
  databaseEngineHelper.InsertALine(s);
}


Can someone write that in VB (without making a mess)? Read it carefully, because it's not as easy as you think! :)
 
I know we shouldn't be using Goto's in vb.net but i havent got time to have a good look through, but here's an example that would work. Mind you my c# is not fantastic.


VB.NET:
Dim s As String
Dim linesRead As Integer=0
s = fileReader.ReadLine()
Do While Not s Is Nothing
  If s.length <> 100 Then
 MessageBox.Show("Line is not right length - ignoring!")
   End If
  If s.startsWith("HEADER") Then
 GoTo Continue1 'ignore header lines
  End If
  If some other test Then
 MessageBox.show("some other test failed too, progressing to next line")
 GoTo Continue1
  End If
  databaseEngineHelper.InsertALine(s)
 Continue1:
 linesRead+=1
 s=fileReader.ReadLine()
Loop
 
incidentally, my best attempt looks like:

VB.NET:
Dim s as String
Dim linesRead as Integer = 0
s = fileReader.ReadLine()
 
While Not s Is Nothing 
  
  if s.length <> 100 then
    MessageBox.Show("Line is not right length - ignoring!")
    linesRead+=1
    s = fileReader.ReadLine()
    Continue While
  End If
 
  if s.startsWith("HEADER") Then
    linesRead+=1
    s = fileReader.ReadLine()
    Continue While
  End If

  if(some other test) Then
    MessageBox.show("some other test failed too, progressing to next line");
    linesRead+=1
    s = fileReader.ReadLine()
    Continue While
  End If
 
  databaseEngineHelper.InsertALine(s);
End While

the actual loop int he program is bigger and not so simplified as presented here. the main issue I have is the way i have to repeat all the post-loop stuf every time i want to continue the while. I cant jsut make it an ElseIfElseIfElseIf sequence because there is code in between the IFs that must run (as noted the loop is more complex)

The For sysntax in VB doesnt go as far as C#, is there any other option? (other than write the class in c#) ? (can i even do this? mix c# and vb source in the same app) ?
 
vis781 said:
I know we shouldn't be using Goto's in vb.net but i havent got time to have a good look through, but here's an example that would work.

I'm actually using a Goto because my best attempt there was just too much of a mess (imho).. yummy yummy! :) And indeed, my Goto effort is identical to yours.. great minds think alike (and fools seldom differ) and all that :)
 
I'm not a c# programmer, but i have a basic knowledge and in my opinion certain programming aspects like the one you have posted above do look more elegant in c#. I do tend to use both from time to time. Can you use them in the same app?? Well i have not considered this, i know that it is possible you instantiate an instance of the c# compiler inside a vb.net app but i don't know if that is it's purpose, but thinking about it, what else could it be for?
 
the loop in the c# code is a For Next loop and you can tell by looking at the 2nd line:
VB.NET:
String s;[FONT=monospace]
[/FONT]for(int linesRead=0, s=fileReader.ReadLine(); s!=null; linesRead+=1, s=fileReader.ReadLine() ){

since cjard didnt post the entire code section, there's no point in converting the code to vb.net

also there are numerous c# to vb.net code converters available to convert things 90% accurately, a simple google search is all you need to find them
 
jug, it's the for loop that i want converting (hence the title of the post). the loop infill is there to highlight the issue i have in vb.net. i know it's a for loop in c# because i wrote it. the purpose of the question is to find out how to express this for loop in vb.net. i dont think it can be done without making a mess because the looping constructs i know of are too retarded in vb.net, so i thought i'd ask the community to see if there were any more advanced constructs i didnt know about

i asked a similar question once and the guy swore i could do this:
VB.NET:
For s = fileReader.ReadLine() to Nothing Step fileReader.Readline()
''code
Next s
I didnt even bother pasting it in, knowing that For requires a numeric variable

I've used 4 converters and every one has failed to convert my loop, or it has made a mess.. I'm just after knowing whether VB.net has any advanced Loop constructs that i dont know about, or do I already know all the loop constructs and they are all just a bit too lacking..

In quite a few respects, i'd say vb.net is like an Apple Mac; nice to look at, and theres some functionality/abstractions that makes it easy to use but the truly powerful stuff is notoriously absent

in c# you can represent all the loops with just for, it's that powerful:

WHILE:
for(; while_test; )

DO:
for(boolean doLoop = true; doLoop; doLoop = test_condition())

VB STYLE FOR:
for(i = 0; i < upper_limit; i++)
for(i = upper_limit; i > 0; i--) 'step -1



I want a similarly powerful loop construct in VB. does it exist? rright now im using While with Goto statements inside. Ugh. :)
 
Last edited:
vb was never designed to be that complex

though that loop can be done in vb, just not with a For loop and all on the same line
 
Back
Top