8

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

7 Answers 7

9

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
Sign up to request clarification or add additional context in comments.

1 Comment

A bit more confusing, yes, but also it would help to keep the code more DRY.
5

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.

Comments

2

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.

3 Comments

Thank you, so I just assign and compare :)
As a prgramming expert, I really hate to use a language for beginners..
Meh. As a programming expert, i recognize that readability > concision. :) IMO VB actually had half a good idea here. Just happened to be a result of other bad ideas.
2

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 

Comments

0

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

Comments

0

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

Comments

0

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

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.