Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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.

Edited by TechnoTone

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

You should never *need* to use IIf.

 

In VB, you might have done:

MsgBox "The lightswitch is: " & IIf(lightswitch.State = lsOn, "On", "Off")

But you can do it just as easily in either language like this:

Dim displayState As String

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

MessageBox.Show("The lightswitch is: " & displayState)

  • *Experts*
Posted

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:

   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:

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:

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Re: IIf and vbNewLine

 

Additionally, I can't find a .NET replacement for string constants such as vbNewLine.

I found one of them. It's Environment.NewLine.

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted
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.

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

  • *Experts*
Posted

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.

Posted

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.

 

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. :)

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

  • *Experts*
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...