MrSomeone
MrSomeone

Reputation: 19

If Operator and Continue For

I have simple loop:

For Each Pla As Player In Game.Players
    Dim JustForTest As String
    JustForTest = If(Pla.Name,  Continue For)
    Console.WriteLine(JustForTest)
Next

If the player's name is nothing, it should skip to the next item(or player), but I got this error at "Continue For":

BC30201 Expression expected.

Of course I can use like this:

For Each Pla As Player In Game.Players

    If Pla.Name = nothing then
    Continue For

    end if
    Console.WriteLine(Pla.Name)
Next

But I'm just curious what I was doing wrong, or is it a bug in VB?

Upvotes: 0

Views: 288

Answers (2)

David Parvin
David Parvin

Reputation: 927

The best way to compare to the Nothing (null in C#) is to use the Is or IsNot comparers, like: If obj Is Nothing Then. If your Name property is supposed to be a string then it is better if you use a String function like String.IsNullOrEmpty().

Your continue For looks correct according to the documentation.

https://msdn.microsoft.com/en-us/library/5z06z1kb.aspx#Anchor_4

Upvotes: -1

TZHX
TZHX

Reputation: 5377

The If Operator expects an Object to be passed into it as arguments, not a control statement. It is meant to be an equivalent to the ternary operator you'll find in other programming languages. You are trying to assign the value Continue For to your JustForTest variable -- and that just doesn't make sense.

It's not a bug in VB, just you trying to use the operator for something it's not designed to do.

Upvotes: 2

Related Questions