Torben
Torben

Reputation: 1290

VB.NET: Assign value to variable inside an IF condition?

is there any possibility to assign a value to a variable inside an IF condition in VB.NET?

Something like that:

Dim customer As Customer = Nothing

If IsNothing(customer = GetCustomer(id)) Then
    Return False
End If

Thank you

Upvotes: 8

Views: 14642

Answers (7)

i00
i00

Reputation: 77

All of the people stating that this is an issue as VB uses the "same operator for both assignment and equality".. I would argue that this is not a good reason as they could simply make slightly different syntax for it... for example: Dim[Name = Expression]

    If a = Dim[qwe = 1 + 2] Then
        'qwe = 3
    End If
'...
    a = 3
    If Dim[qwe = a = 1 + 2] Then
        'qwe = True
    End If

Upvotes: 0

Wolfgang Grinfeld
Wolfgang Grinfeld

Reputation: 1008

Yes, it is possible to assign a value to a variable inside an IF condition in VB.NET. There is even builtin support for doing this, albeit to a limited extend:

If Interlocked.Exchange(customer, GetCustomer(id)) Is Nothing Then
    Return False
End If

where the methods within the Interlocked class are intended to be used within a multi-threading environment. By design, the return value of Exchange() is the old value, rather than the new one. Thus, to obtain the rather cryptic equivalent result:

If (customer = Interlocked.Exchange(customer, GetCustomer(id)) And customer Is Nothing Then
    return false
End If

Upvotes: 0

Brian Gideon
Brian Gideon

Reputation: 48949

There is no builtin support for doing this, but you could use this workaround.

Public Function Assign(Of T)(ByRef destination As T, ByVal value As T) As T
  destination = value
  Return destination
End Function

And then it could be used like this.

Dim customer As Customer = Nothing 

If IsNothing(Assign(customer, GetCustomer(id))) Then 
    Return False 
End If 

Upvotes: 2

dbasnett
dbasnett

Reputation: 11773

Thinking out loud because you didn't show Customer or GetCustomer...

        Dim myCust As New Customer
        If myCust.GetCustomer(someID) Then

        End If

Class Customer

    Private _customerID As Integer
    Const noCustomer As Integer = -1

    Public Sub New()
        Me._customerID = noCustomer 'no customer
    End Sub

    Public Function GetCustomer(ByVal id As Integer) As Boolean
        'perform required action to get customer
        'if successful then set Me._customerID to ID else set it to no customer value
        Return Me.HaveCustomer
    End Function

    Public Function HaveCustomer() As Boolean
        If Me._customerID = noCustomer Then Return False Else Return True
    End Function
End Class

Upvotes: 0

MrUpsideDown
MrUpsideDown

Reputation: 639

No, I'm fairly sure this is not possible - but be thankful!

This is a "feature" of C-based languages that I would never encourage the use of, because it is probably misused or misinterpreted more often than not.

I think the best approach is to try and express "one thought per line", and resist coding that combines two operations in the same line. Combining an assignment and a comparison in this way usually doesn't achieve much other than making the code more difficult to understand.

Upvotes: 5

Mike Caron
Mike Caron

Reputation: 14561

Sorry, no. On the other hand, it would get really confusing, since VB.NET uses the same operator for both assignment and equality.

If a = b Then 'wait, is this an assignment or comparison?!

Instead, just set the variable and compare:

Dim customer As Customer = Nothing

customer = GetCustomer(id)

If IsNothing(customer) Then
    Return False
End If

Upvotes: 9

cHao
cHao

Reputation: 86506

VB doesn't do that very well, especially since assignment and comparison both use the = operator. It'd get really confusing. And since VB to some degree is designed to be newbie-friendly (The B in "BASIC" actually stands for "Beginner's"), expressions with multiple side effects are not something the language generally likes to do.

If it's essential that you be able to do it all in one line, then you could make the GetCustomer method assign to a ByRef parameter, and return a boolean saying whether the assigned value was null. But really, it's better to just assign and then compare to null.

Upvotes: 2

Related Questions