|
-
May 6th, 2007, 02:29 PM
#1
Thread Starter
Lively Member
[2005] The Conditional Operator - what's the VB.NET equivalent?
Hey there,
C and many other languages have this construct:
Code:
variable = (condition) ? value1 : value2
Where variable is assigned value1 if (condition) returns true, otherwise value2 is assigned. What is the VB equivalent? I'd really rather not use an If...Else...End If statement.
Gr,
Mightor
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 6th, 2007, 02:34 PM
#2
Lively Member
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
are you opposed to select case statement?
-
May 6th, 2007, 02:38 PM
#3
Thread Starter
Lively Member
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
 Originally Posted by bobdabilder
are you opposed to select case statement?
Not opposed, it just seems like so much code for such a simple operation The beauty of the conditional operator is its compactness.
Gr,
Xander
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 6th, 2007, 02:49 PM
#4
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
You can use an IIf but it is part of the Microsoft.VisualBasic.Interaction class. 
variable = IIf(Something = True, "True", "False")
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 6th, 2007, 03:04 PM
#5
Thread Starter
Lively Member
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
 Originally Posted by RobDog888
You can use an IIf but it is part of the Microsoft.VisualBasic.Interaction class. 
variable = IIf(Something = True, "True", "False")
Why the sad face? Is using that class not a good thing? I remember reading about the IIf operator but I couldn't find much about it in my MSDN, except for the jscript language and VB was telling me it was an unknown operator.
Gr,
Mightor
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 6th, 2007, 03:07 PM
#6
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
IIF is part of the imported ms.vb namespace which was imcluded first with 2003 in efforts to help vb6 programmers make the transition to .net. Its not .net code but rather a wrapper for pure .net code calls in the background I believe. Just a matter of preference I guess but who knows when ms will leave it out of future versions.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 6th, 2007, 03:08 PM
#7
Fanatic Member
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
I use the IIf method all over my program and I've never had any problems with it. Are there any downsides to using it that I should know about? I know that the MsgBox method comes from the Microsoft.VisualBasic.Interaction namespace but shouldn't be used...
-
May 6th, 2007, 03:13 PM
#8
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
There is no real downside to using it (other then miniscule performance loss and being viewed as not .net code). Its just something to remember that if ms drops the ms.vb class from future versions it will break your code etc. Depending on the amount of use in your app you may have to spend un-necessary time changing it to .net code.
Simple prevention is easy.
Use Messagebox.Show instead of MsgBox.
Use a If something Then code block (which also includes "AndAlso" for multiple conditioning).
etc.
Then no need to ever worry about those parts at least.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 6th, 2007, 03:19 PM
#9
Fanatic Member
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
The IIf function is quite easy to replicate anyway, if it is ever removed:
Code:
Public Function IIf(ByVal condition As Boolean, ByVal trueValue As Object, ByVal falseValue As Object) As Object
If condition Then
Return trueValue
Else
Return falseValue
End If
End Function
-
May 6th, 2007, 03:21 PM
#10
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
True but the point is "prevention". You are typing it out already and know how to replace it so why not just do it now?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 6th, 2007, 03:25 PM
#11
Fanatic Member
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
Why get a dog and bark yourself?
-
May 6th, 2007, 07:09 PM
#12
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
There is no VB equivalent to the ?: operator in C# and other C-based languages. The IIf function performs a similar job but it is a function, not an operator. That is a VERY important distinction. In C# this would work:
C# Code:
string str = null;
int len = (str == null ? 0 : str.Length);
while in VB this would not:
vb Code:
Dim str As String = Nothing
Dim len As Integer = IIf(str Is Nothing, 0, str.Length)
The reason for that is that the ?: operator short circuits, which means the false part is never evaluated if the expression is true. In VB the IIf function is called with three parameters, all of which must be evaluated. That means that the False part is evaluated no matter what. In the code above str.Length would throw a NullReferenceException, so the VB code would crash.
-
May 6th, 2007, 07:33 PM
#13
Re: [2005] The Conditional Operator - what's the VB.NET equivalent?
Another serious problem with IIf is that the result is 'Object', which can cause many problems that aren't caused by a true (ternary) conditional operator.
e.g.,
Dim str As String = Nothing
Dim len As Integer = IIf(str Is Nothing, "d", 0)
This will compile in VB, but fail at run-time. A true ternary conditional operator would cause a compiler error in this situation.
A true ternary operator is in the works for VB9. See the following link:
http://www.panopticoncentral.net/arc...x?Pending=true
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|