[2005] The Conditional Operator - what's the VB.NET equivalent?
Hey there,
C and many other languages have this construct:
Code:
variable = (condition) ? value1 : value2
Where variable is assigned value1 if (condition) returns true, otherwise value2 is assigned. What is the VB equivalent? I'd really rather not use an If...Else...End If statement.
Gr,
Mightor
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
are you opposed to select case statement?
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
Quote:
Originally Posted by bobdabilder
are you opposed to select case statement?
Not opposed, it just seems like so much code for such a simple operation :) The beauty of the conditional operator is its compactness.
Gr,
Xander
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
You can use an IIf but it is part of the Microsoft.VisualBasic.Interaction class. :(
variable = IIf(Something = True, "True", "False")
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
Quote:
Originally Posted by RobDog888
You can use an IIf but it is part of the Microsoft.VisualBasic.Interaction class. :(
variable = IIf(Something = True, "True", "False")
Why the sad face? Is using that class not a good thing? I remember reading about the IIf operator but I couldn't find much about it in my MSDN, except for the jscript language and VB was telling me it was an unknown operator.
Gr,
Mightor
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
IIF is part of the imported ms.vb namespace which was imcluded first with 2003 in efforts to help vb6 programmers make the transition to .net. Its not .net code but rather a wrapper for pure .net code calls in the background I believe. Just a matter of preference I guess but who knows when ms will leave it out of future versions. ;)
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
I use the IIf method all over my program and I've never had any problems with it. Are there any downsides to using it that I should know about? I know that the MsgBox method comes from the Microsoft.VisualBasic.Interaction namespace but shouldn't be used...
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
There is no real downside to using it (other then miniscule performance loss and being viewed as not .net code). Its just something to remember that if ms drops the ms.vb class from future versions it will break your code etc. Depending on the amount of use in your app you may have to spend un-necessary time changing it to .net code.
Simple prevention is easy.
Use Messagebox.Show instead of MsgBox.
Use a If something Then code block (which also includes "AndAlso" for multiple conditioning).
etc.
Then no need to ever worry about those parts at least. ;)
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
The IIf function is quite easy to replicate anyway, if it is ever removed:
Code:
Public Function IIf(ByVal condition As Boolean, ByVal trueValue As Object, ByVal falseValue As Object) As Object
If condition Then
Return trueValue
Else
Return falseValue
End If
End Function
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
True but the point is "prevention". You are typing it out already and know how to replace it so why not just do it now? :D
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
Why get a dog and bark yourself? :D
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
There is no VB equivalent to the ?: operator in C# and other C-based languages. The IIf function performs a similar job but it is a function, not an operator. That is a VERY important distinction. In C# this would work:
C# Code:
string str = null;
int len = (str == null ? 0 : str.Length);
while in VB this would not:
vb Code:
Dim str As String = Nothing
Dim len As Integer = IIf(str Is Nothing, 0, str.Length)
The reason for that is that the ?: operator short circuits, which means the false part is never evaluated if the expression is true. In VB the IIf function is called with three parameters, all of which must be evaluated. That means that the False part is evaluated no matter what. In the code above str.Length would throw a NullReferenceException, so the VB code would crash.
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
Another serious problem with IIf is that the result is 'Object', which can cause many problems that aren't caused by a true (ternary) conditional operator.
e.g.,
Dim str As String = Nothing
Dim len As Integer = IIf(str Is Nothing, "d", 0)
This will compile in VB, but fail at run-time. A true ternary conditional operator would cause a compiler error in this situation.
A true ternary operator is in the works for VB9. See the following link:
http://www.panopticoncentral.net/arc...x?Pending=true