Xaisoft
Xaisoft

Reputation: 46591

Is this similar to the ternary operator in vb.net?

I saw this in some vb.net source code:

Dim sTest As String = "" & drTest("column")

I was told that if drTest("column") is nothing, then sTest will be assigned "", so it is in effect doing:

Dim sTest As String = If("",Nothing,drTest("column"))

What is the downside of doing it the first way I showed?

What is the difference between using If and IIf?

Upvotes: 2

Views: 1266

Answers (2)

Code Maverick
Code Maverick

Reputation: 20415

While @Konrad Rudolph's answered your specific question, I'd like to continue where he left off saying:

This makes no sense and shouldn’t even compile (or at least give a warning). Your first code is fine – it works always and there is no downside. It’s not clear what the second code is even trying to achieve.

He's right in that you shouldn't even be doing what your second line is suggesting. The simple answer that achieves the effect you are trying for, would be to use TryCast instead. You are only assigning a non-empty string value when drTest("column") contains a value. Since that's the case, why not do:

Dim sTest As String = TryCast(drTest("column"), String)

This way you get your value if the TryCast succeeds, otherwise you get Nothing.

Now since TryCast returns Nothing and since your example showed that you wanted to assign "", you might not want to go this route, as it can lead into the frustration of handling String Nothingness vs "" / String.Empty. You could use the String.IsNullOrEmpty() method if you want, but that requires you to pass in your string as a parameter and, to me, that clunks up your code. Alternatively, you could create your own StringExtensions Module like I have and use the extensions to check Nothingness and Emptiness:

Option Explicit On
Option Strict On

Imports System.Runtime.CompilerServices

Public Module StringExtensions

    <Extension()> _
    Public Function IsNullOrEmpty(ByVal s As String) As Boolean
        Return s Is Nothing OrElse s.Trim.Length.Equals(0)
    End Function

    <Extension()> _
    Public Function IsNotNullOrEmpty(ByVal s As String) As Boolean
        Return s IsNot Nothing AndAlso s.Trim.Length > 0
    End Function

End Module

Then to achieve your effect of setting "" with respect to Nothingness, one could use the combination of the TryCast and my StringExtensions methods to do:

With ternary If

Dim sColumn1 As String = TryCast(drTest("column1"), String)
Dim sColumn2 As String = TryCast(drTest("column2"), String)
Dim sColumn3 As String = TryCast(drTest("column3"), String)

Dim sTest1 As String = If(sColumn1.IsNotNullOrEmpty, sColumn1, String.Empty)
Dim sTest2 As String = If(sColumn2.IsNotNullOrEmpty, sColumn2, String.Empty)
Dim sTest3 As String = If(sColumn3.IsNotNullOrEmpty, sColumn3, String.Empty)

Without ternary If

Dim sTest1 As String = String.Empty
Dim sTest2 As String = String.Empty
Dim sTest3 As String = String.Empty

Dim sColumn1 As String = TryCast(drTest("column1"), String)
Dim sColumn2 As String = TryCast(drTest("column2"), String)
Dim sColumn3 As String = TryCast(drTest("column3"), String)

If sColumn1.IsNotNullOrEmpty Then sTest1 = sColumn1
If sColumn2.IsNotNullOrEmpty Then sTest2 = sColumn2
If sColumn3.IsNotNullOrEmpty Then sTest3 = sColumn3

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545568

If is the ternary conditional operator.

What is the difference between using If and IIf?

IIf is an ordinary function that is implemented approximately as follows:

Function If(Of T)(condition As Boolean, ifTrue As T, ifFalse As T)
    If condition Then Return ifTrue
    Return ifFalse
End Function

This means that no matter the value of condition, both its other arguments are always evaluated. This doesn’t happen with the If operator.

But your code is actually doing something completely different:

Dim sTest As String = If("",Nothing,drTest("column"))

This makes no sense and shouldn’t even compile (or at least give a warning). Your first code is fine – it works always and there is no downside. It’s not clear what the second code is even trying to achieve.

Upvotes: 7

Related Questions