Results 1 to 13 of 13

Thread: [2005] The Conditional Operator - what's the VB.NET equivalent?

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

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

  2. #2
    Lively Member
    Join Date
    Feb 2006
    Posts
    125

    Re: [2005] The Conditional Operator - what's the VB.NET equivalent?

    are you opposed to select case statement?

  3. #3

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] The Conditional Operator - what's the VB.NET equivalent?

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

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] The Conditional Operator - what's the VB.NET equivalent?

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

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    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...
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    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
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  11. #11
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: [2005] The Conditional Operator - what's the VB.NET equivalent?

    Why get a dog and bark yourself?
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. string str = null;
    2. int len = (str == null ? 0 : str.Length);
    while in VB this would not:
    vb Code:
    1. Dim str As String = Nothing
    2. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    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
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width