DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Exploring Operator, OpenAI’s New AI Agent
  • Building a Sample Kubernetes Operator on Minikube: A Step-by-Step Guide
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators
  • Spring WebFlux: publishOn vs subscribeOn for Improving Microservices Performance

Trending

  • How to Use Testcontainers With ScyllaDB
  • Enterprise-Grade Distributed JMeter Load Testing on Kubernetes: A Scalable, CI/CD-Driven DevOps Approach
  • Kung Fu Commands: Shifu Teaches Po the Command Pattern with Java Functional Interfaces
  • The Rise of Self‐Service Platforms: How Cloud Development Environments Are Reshaping Dev Culture

Ternary Operator in VB.NET

By 
Jalpesh Vadgama user avatar
Jalpesh Vadgama
·
Apr. 10, 12 · Interview
Likes (1)
Comment
Save
Tweet
Share
33.9K Views

Join the DZone community and get the full member experience.

Join For Free

We all know about the ternary operator in C#.NET. I am a big fan of the 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.

VB.NET Operator (extension)

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Operator, OpenAI’s New AI Agent
  • Building a Sample Kubernetes Operator on Minikube: A Step-by-Step Guide
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators
  • Spring WebFlux: publishOn vs subscribeOn for Improving Microservices Performance

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: