Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

I am Jalpesh Vadgama a Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 6 years of experience in .NET Technology.
Browse by Tags · View All
#ASP.NET 88
#DOTNET 87
BRH 79
ASP.NET 72
#.NET 52
C#.NET 48
DOTNET 47
ASP.NET 4.0 31
ASP.NET MVC 29
VisualStudio 27

Archive · View All
December 2010 16
July 2011 13
April 2011 13
April 2012 12
January 2011 12
June 2011 11
May 2011 11
May 2012 8
February 2013 7
January 2013 7

Ternary operator in VB.NET

Apr 4 2012 4:34AM by Jalpesh   

We all know about Ternary operator in C#.NET. I am a big fan of ternary operator and I like to use it instead of using IF..Else. Those who don’t know about ternary operator please go through below link.

http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx

Here you can see ternary operator returns one of the two values based on the condition. See following example.

bool value = false;
string output=string.Empty;
 
//using If condition
if (value==true)
  output ="True";
else
  output="False";
 
//using tenary operator
output = value == true ? "True" : "False";

In the above example you can see how we produce same output with the ternary operator without using If..Else statement.

Recently in one of the project I was working with VB.NET language and I was eager to know if there is a ternary operator equivalent there or not. After searching on internet I have found two ways to do it. IF operator which works for VB.NET 2008 and higher version and IIF operator which is there since VB 6.0.

So let’s check same above example with both of this operators. So let’s create a console application which has following code.

Module Module1
 
  Sub Main()
      Dim value As Boolean = False
      Dim output As String = String.Empty
 
      ''Output using if else statement
      If value = True Then
          output = "True"
      Else
          output = "False"
 
          Console.WriteLine("Output Using If Loop")
          Console.WriteLine(output)
 
          output = If(value = True, "True", "False")
 
          Console.WriteLine("Output using If operator")
          Console.WriteLine(output)
 
          output = IIf(value = True, "True", "False")
 
          Console.WriteLine("Output using IIF Operator")
          Console.WriteLine(output)
 
          Console.ReadKey()
 
      End If
  End Sub
 
End Module

As you can see in the above code I have written all three-way to condition check using If.Else statement and If operator and IIf operator. You can see that both IIF and If operator has three parameter first parameter is the condition which you need to check and then another parameter is true part of you need to put thing which you need as output when condition is ‘true’. Same way third parameter is for the false part where you need to put things which you need as output when condition as ‘false’.

Now let’s run that application and following is the output as expected.

Ternary Operator in Vb.NET

That’s it. You can see all three ways are producing same output. Hope you like it. Stay tuned for more..Till then Happy Programming.

Shout it

Tags: C#.NET, ASP.NET, VB.NET,


Jalpesh
15 · 11% · 3548
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

3  Comments  

  • Hi Jalpesh, thank you for posting this. I'm sorry but I think you're only partially right. In VB.Net, IIf() is a function, not an operator. Like any other function in VB.Net, all parameters are evaluated before the function returns its value (or even executes its implementation)!

    The impact will be clear if you run the following code:

    Module Module1

    Sub Main()
    
        Dim theStopWatch As New Stopwatch
    
    
        Dim value As Boolean = False
        Dim output As String = String.Empty
    
        ''Output using if else statement
        theStopWatch.Reset()
        theStopWatch.Start()
        If value = True Then
            output = HeavyFunctionReturnsTrue()
        Else
            output = LightFunctionReturnsFalse()
        End If
        Console.WriteLine("Output using If-Then-Else statement")
        Console.WriteLine(output)
        Console.WriteLine("It took {0} milliseconds to complete", theStopWatch.ElapsedMilliseconds.ToString())
        Console.WriteLine()
    
        theStopWatch.Reset()
        theStopWatch.Start()
        output = If(value = True, HeavyFunctionReturnsTrue, LightFunctionReturnsFalse)
        Console.WriteLine("Output using If operator")
        Console.WriteLine(output)
        Console.WriteLine("It took {0} milliseconds to complete", theStopWatch.ElapsedMilliseconds.ToString())
        Console.WriteLine()
    
        theStopWatch.Reset()
        theStopWatch.Start()
        output = IIf(value = True, HeavyFunctionReturnsTrue, LightFunctionReturnsFalse)
        Console.WriteLine("Output using IIF function")
        Console.WriteLine(output)
        Console.WriteLine("It took {0} milliseconds to complete", theStopWatch.ElapsedMilliseconds.ToString())
    
        Console.ReadKey()
    
    End Sub
    
    
    ''' <summary>
    ''' Function that takes a long time to complete
    ''' </summary>
    ''' <returns>"True"</returns>
    ''' <remarks></remarks>
    Private Function HeavyFunctionReturnsTrue() As String
        System.Threading.Thread.Sleep(5000)
        Return "True"
    End Function
    
    ''' <summary>
    ''' Function that takes little time to complete
    ''' </summary>
    ''' <returns>"False"</returns>
    ''' <remarks></remarks>
    Private Function LightFunctionReturnsFalse() As String
        System.Threading.Thread.Sleep(10)
        Return "False"
    End Function
    

    End Module

    The results are the same but the IIf() function evaluates both functions, no matter what the condition is. However, the If -operator only evaluates the parameter it needs, depending on the condition. So the If -operator is a true conditional operator.

    In my opinion, for maintainability and readability reasons it's hardly ever (if not never) a good idea to use it in VB.Net.

    Regards, Sebastiaan

    commented on Apr 18 2012 7:53AM
    Sebastiaan
    765 · 0% · 41
  • HI Sebastiaan

    Here i was talking about alternative to Ternary operator in VB.NET as it was not having such operator that we have in C#.

    Regards, Jalpesh

    commented on Apr 18 2012 8:51AM
    Jalpesh
    15 · 11% · 3548
  • Hi Jalpesh,

    The If operator in VB.Net is a true ternary operator, taking 3 arguments, and evaluating the 2nd or 3d (but never both!) based on the 1st (which is the condition). It works in exactly the same way as the ?: operator in C# and some other languages. When translating C# code to VB.Net you can replace the ?: operator with the If operator.

    Regards,

    Sebastiaan

    commented on Apr 18 2012 12:46PM
    Sebastiaan
    765 · 0% · 41

Your Comment


Sign Up or Login to post a comment.

"Ternary operator in VB.NET" rated 5 out of 5 by 2 readers
Ternary operator in VB.NET , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]