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

is it possible 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 

thanks

+5
source share
6 answers

Sorry no. This, on the other hand, will be very confusing since VB.NET uses the same operator for 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 
+6
source

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

This is a “feature” of C-based languages ​​that I would never encourage using, because it is probably misused or misinterpreted more often.

I think the best approach is to try to express “one thought in a line” and resist coding that combines two operations on one line. Combining a task and comparing in this way usually does not achieve much more than make the code more difficult to understand.

+4
source

VB does not do this very well, especially since assignment and comparison use the = operator. It will be very confusing. And since VB is to some extent intended for beginners (The B in "BASIC" actually means "Beginner's"), expressions with a few side effects are not what people usually like.

If you need to do everything in one line, you can make the GetCustomer method assigned to the ByRef parameter and return a logical expression indicating whether the assigned value was empty. But in fact, it is better to simply assign and then compare with zero.

+3
source

There is no built-in support for this, but you can 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 can be used like that.

 Dim customer As Customer = Nothing If IsNothing(Assign(customer, GetCustomer(id))) Then Return False End If 
+2
source

Thinking out loud because you haven’t shown 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 
0
source

Yes, you can assign a value to a variable inside an IF condition in VB.NET. There is even built-in support for this, although to a limited extent:

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

where the methods in the Interlocked class are intended for use in a multi-threaded environment. In design, the return value of Exchange () is the old value, not the new one. Thus, to get a rather cryptic equivalent result:

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

All Articles