Phil,
If your on VS 2005, check out my generic IIf (link previously posted), you
can simplify your statement to:
| Dim PurchaseType As String _
| = IIf( drwData.Item("DailyItem").Equals( "True" ) _
| , "ID" _
| , "PID" _
| )
The above line is with Option Strict On! My generic IIf uses generics & type
inference to "create" a "IIf(boolean, String, String) As String" as needed
and call it, no ugly DirectCast needed, no boxing of value types. Totally
Option Strict On friendly!
Of course there may be side effects...
Consider the following:
' note we are explicitly setting the seed values in each case to
' ensure we get the same sequence of pseudo random numbers back.
Dim falseCounter As New Random(1)
Dim trueCounter As New Random(2)
Debug.WriteLine(IIf(True, trueCounter.Next(), falseCounter.Next()),
"true")
Debug.WriteLine(IIf(False, trueCounter.Next(), falseCounter.Next()),
"false")
In VB it displays:
true: 1655911537
false: 237820880
While in alleged direct equivalent in C#:
Random falseCounter = new Random(1);
Random trueCounter = new Random(2);
Debug.WriteLine(true ? trueCounter.Next() : falseCounter.Next(),
"true");
Debug.WriteLine(false ? trueCounter.Next() :
falseCounter.Next(), "false");
Displays:
true: 1655911537
false: 534011718
Notice that the second line doesn't match, this is because VB called the
falseCounter.Next function twice (once on each line), while C# only called
it once (only on the second line).
If instead of IIf we use the code that Sean gave:
Dim value As Integer
If True Then
value = trueCounter.Next()
Else
value = falseCounter.Next()
End If
Debug.WriteLine(value, "true")
If False Then
value = trueCounter.Next()
Else
value = falseCounter.Next()
End If
Debug.WriteLine(value, "false")
We then get the same results as C#:
true: 1655911537
false: 534011718
Consider what happens if instead of calling Random.Next() you are calling a
GetNextSequenceNumber function, that simply increments a counter & return
it,
id = IIf(sequenceAlreadyExists, theExistingSequence,
GetNextSequenceNumber())
For example you are creating records from some input source and if the
record doesn't have an ID you call GetNextSequenceNumber to assign a new
one. The above line in VB will cause gaps in the sequence number, as is
called for every "line" in the input source. Consider what happens if the
GetNextSequenceNumber() function is fairly expensive (performance, memory
pressure, network, other) to execute.
As I stated, in the case of constants like the OP & your example, using IIf
doesn't hurt, however I strongly recommend any one using IIf to understand
what it is actually doing!
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
| | >I need to convert some C# ?Conditionals over to vb.net
| >
| > string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
| > "True") ? "ID" : "PID";
|
| The direct equivalent of the "?" operator is Visual Basic's IIf function:
|
| Dim PurchaseType As String _
| = DirectCast( IIf( drwData.Item("DailyItem").Equals( "True" ) _
| , "ID" _
| , "PID" _
| ), String )
|
| (line breaks for clarity(?) only)
|
| HTH,
| Phill W.
|
|