IIf

TechnoTone

Junior Contributor
Joined
Jan 20, 2003
Messages
224
Location
UK - London
IIf and vbNewLine

Is there a .NET equivalent to the "IIf" function (without resorting to the Microsoft.VisualBasic namespace)?

Additionally, I can't find a .NET replacement for string constants such as vbNewLine.
 
Last edited:
I'm not sure about IIf, but the replacement for constants such as
vbNewLine and vbTab can be found in the ControlChars class.
The equivalent of vbNewLine or vbCrlf is ControlChars.CrLf.

Keep in mind that using the VisualBasic namespace isn't always a
bad thing. If there is no replacement for IIf, you shouldn't feel
"guilty" using it, just so long as you don't use ALL the old VB
methods in your projects. At least that's MY opinion.
 
You should never *need* to use IIf.

In VB, you might have done:
Visual Basic:
MsgBox "The lightswitch is: " & IIf(lightswitch.State = lsOn, "On", "Off")
But you can do it just as easily in either language like this:
Visual Basic:
Dim displayState As String

If lightswitch.State = lsOn Then _
  displayState = "On" _
Else displayState = "Off"

MessageBox.Show("The lightswitch is: " & displayState)
 
Using IIf is slow because its output isn't strongly typed, and it's generally regarded as bad programming practice.
 
IIf is bad programming practice in VB6? Is it? I use it quite often because it save the lines of source.

Why you said so?
 
Although is only in one line, but how do you know 'IIf' must check both cases? Why it cannot like "If... then... else" statement?
 
IIf in VB6 is also slow because it uses variants. There is no excuse for using it, just use a structured If.. Then.. Else statement instead.
 
You can test that IIF (at least in VB6) checked both cases by putting in a MsgBox in the True and False parts. Run the following and you'll see both message boxes:
Visual Basic:
    Dim Result As Long
    Result = IIf(True, MsgBox("True"), MsgBox("False"))

It's a common VB6 "bug" that you should know about if you use IIF. It also makes it harder to comment what each case is.

I have used the C++ style "IIF" in VB6. It only works if you need to return true/false, such as to set an Enabled property. Suppose you have a Recordset and you need to disable a save button if there are no records:
Visual Basic:
cmdSave.Enabled = (rs.RecordCount = 0)

It evaluates the expression on the right and returns true/false which is assigned to the Enabled property.

Does anyone know if VB6 supports the C# syntax for the ( ? : ) evaluation? It's the equivalent for IIf but doesn't evaluate both sides and is strongly typed. You'd use it as:
Code:
int i = 5;
int j = (i < 10 ? 42 : 43);

I use it on occasion where the true and false parts are simple. If they are more than 1/3 of my screen, I break it into if()...else...

-Nerseus
 
ControlChars is part of the Microsoft.VisualBasic namespace. That is what I'm trying to avoid. I'm not completely against using the VisualBasic namespace but if there is an alternative then I will avoid it.
 
Microsoft.VisualBasic is not a bad thing. Especially for just
constants. You don't want to use the Functions that are provided
as backwards compatibility for VB6, but constants are perfectly fine.
They are exactly the same no matter where they are located in
the framework hierarchy.

Microsoft.VisualBasic is actually not a bad thing to use, if you use it
properly.
 
Sorry to be flogging a dead horse here but I just want to make myself clear. I understand what you're saying but the difficulty is in knowing whether what you know exists in the VisualBasic namespace also exists within .NET. It's going to take time until I've figured out where the line between .NET and VisualBasic exists but until then I won't use VisualBasic until I'm satisfied that there's no alternative.

VolteFace said:
Microsoft.VisualBasic is actually not a bad thing to use, if you use it properly.
Exactly - but how does a beginner know what is proper?



I'm sure I'll get there eventually. :)
 
If there is a namespace in the framework that will do the same thing,
then it is more likely better. For example, use the System.IO
namespace over the functions in the VisualBasic namespace any day.
However, things like constants and enumerations are definately
alright to use; they are just raw values. They will behave exactly
the same no matter where they are stored.
 
Back
Top