1

I have some little issues with syntax. I am used to C# and just started VB.NET. I am trying to do a while loop when it loops through the items from a list. What is the syntax that I am doing wrong?

While oResponse.outputControl.Items(i) <> Nothing

    //Do something

End While
4
  • 3
    The problem is the <> Nothing part, try Is Nothing instead. Commented Jan 20, 2012 at 16:44
  • Operator '<>' is not defined for types 'System.Web.UI.Webcontrols.ListItem and 'System.Web.UI.WebControls.ListItem' Commented Jan 20, 2012 at 16:44
  • But Is is the opposite of your original code. I would think you want the IsNot operator instead. Commented Jan 23, 2012 at 15:13
  • The Is allowed to to find IsNot I did not know the operators for vb I am used to C# so it was a stepping stone. Commented Jan 23, 2012 at 19:09

2 Answers 2

6

Don't forget to increment your counter:

While oResponse.outputControl.Items(i) <> Nothing

    'Do something
    i += 1

End While

and if this is a reference type (you didn't say, but it probably is), you can't compare it to Nothing with the <> operator:

While oResponse.outputControl.Items(i) IsNot Nothing

    'Do something
    i += 1

End While

But maybe what you really want is a For Each loop:

For Each Item In oResponse.outputControl.Items
    'Do Something
Next Item

And one more thing: what's with the hungarian wart in the oResponse variable? That style is no longer recommended, and Microsoft now even specifically recommends against it. VB.Net also has a feature called "Default properties" that just might make this even simpler. Pulling it all together (now including the ListItem type from your comment above):

For Each Item As ListItem In Response.outputControl
    'Do Something
Next Item
Sign up to request clarification or add additional context in comments.

1 Comment

"And one more thing: what's with the hungarian wart in the oResponse variable?" Not my code. I just started working on the code yesterday. Right now I am focusing on functionality and then going back to change all that kind of stuff.
0

Try:

While oResponse.outputControl.Items(i) Is Nothing

    'do something

End While

You could also do this if you wanted to be particularly old school:

Do

    'do something

Loop Until Not oResponse.outputControl.Items(i) Is Nothing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.