TechnoTone Posted January 24, 2003 Posted January 24, 2003 (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 January 24, 2003 by TechnoTone Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
*Experts* Bucky Posted January 24, 2003 *Experts* Posted January 24, 2003 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. Quote "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* Volte Posted January 24, 2003 *Experts* Posted January 24, 2003 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) Quote
*Gurus* divil Posted January 24, 2003 *Gurus* Posted January 24, 2003 Using IIf is slow because its output isn't strongly typed, and it's generally regarded as bad programming practice. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
TechnoTone Posted January 24, 2003 Author Posted January 24, 2003 Thanks. :) Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
bungpeng Posted January 28, 2003 Posted January 28, 2003 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? Quote
Moderators Robby Posted January 28, 2003 Moderators Posted January 28, 2003 IIF must check both cases, IF only checks the ELSE if the IF is not true. Makes sense? :) Quote Visit...Bassic Software
bungpeng Posted January 28, 2003 Posted January 28, 2003 Although is only in one line, but how do you know 'IIf' must check both cases? Why it cannot like "If... then... else" statement? Quote
*Gurus* divil Posted January 28, 2003 *Gurus* Posted January 28, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted January 28, 2003 *Experts* Posted January 28, 2003 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 Quote "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
TechnoTone Posted January 31, 2003 Author Posted January 31, 2003 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. Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Moderators Robby Posted January 31, 2003 Moderators Posted January 31, 2003 Bucky had already told you how to do it in his first post. ControlChars Quote Visit...Bassic Software
TechnoTone Posted January 31, 2003 Author Posted January 31, 2003 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. Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
*Experts* Volte Posted January 31, 2003 *Experts* Posted January 31, 2003 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. Quote
TechnoTone Posted January 31, 2003 Author Posted January 31, 2003 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. :) Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
*Experts* Volte Posted January 31, 2003 *Experts* Posted January 31, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.