102 Comments

The IIF-statements in VB.NET are a handy way to shorten our code and to make it more readable. It’s also an easy way to introduce subtle bugs into our systems.

Given the following code:

        Dim amount = 100

        IIf(amount = 100, amount = 30, amount = 50)
        Console.WriteLine(amount)

When run, the app will print out 30, yes? Well, actually no. Here’s the actual output when the app is executed:

image

What happens is that the code inside the true-part of the IIF-statement is not an assignment.Instead, it’s interpreted by the compiler as aboolean expression. Now let’s try to modify our code so that it stores the return value of the IIF-statement in a variable:

        Dim amount = 100

        Dim outPutValue = IIf(amount = 100, amount = 30, amount = 50)
        Console.WriteLine(amount)
        Console.WriteLine(outPutValue)

This is the output when executed:

image

As you can see, the IIF statement returns False, because it compares the equality of variable “amount” to value 30.

To correct the above code one should change it to the following:

        Dim amount = 100

        amount = IIf(amount = 100, 30, 50)
        Console.WriteLine(amount)

Bottom line: IIF-statements in VB.NET shouldn’t be used for assignments.

Comments

Comment by free pdf ebooks download

free pdf ebooks download...

www.wurpdf.com is a search engine which is used to find different eBooks, manuals and other types of electronic documents in various sites....

Comment by download pdf books

download pdf books...

www.mikbooks is a search engine which is used to find different eBooks, manuals and other types of electronic documents in various sites....

Comment by new balance outlet store

new balance outlet store...

I am curious to find out what blog platform you have been using? I'm having some small security problems with my latest website and I would like to find something more risk-free. Do you have any suggestions?...

Comment by new balance新作

new balance新作...

の部屋が 私は入力した後にビング からサイトおそらく(かなり正確に覚えていない)似たようなことか|に}入力された{か。あなたの対象材料は | というだけの理由ので私は(大学紙)用を検索し、私は私が集めるいくつかのここから材料とリファレンス。 感謝。...

Comment by Order women flint 13 Online

Order women flint 13 Online...

dugunazrqp jordan 13 retro altitude Shop jordan 23 white varsity Red Buy last shot 14s air jordan 1 retro high strap For Sale bcairfilter.com/wordss.php order Jordan 23 Motorsports black ...

Comment by New white cement 3s

New white cement 3s...

cheap powder blue 9s pffzij jordan 6 gs oreo Gradeschool Buy retro cardinals kids jordan 7 cardinals kids Authentic concord 11s For Sale www.phprc.net/keywords.asp xobheltmaox Holiday Jordan Release Dates 20...

Comment by 2015 new Nike Dunk Low White Black Blue Purple 2014

2015 new Nike Dunk Low White Black Blue Purple 2014...

fxiqucvadak Buy Authentic Real Nike Free 5.0 WMS 510643-601 Red Raspberry Volt 2014 online real Nike Air Max Tailwind 2010 Dark Red August Releases for sale awrphqio Nike 104265-053 2014 buy cheap Nike Lunar Eclipse 2 Grey Green Purple sneakers for men...

Comment by jordan 6 sport blue

jordan 6 sport blue...

playing in front of a hot goalie and responding to perhaps Mike Babcock's best coaching job. That...

Comment by jordan 6 sport blue

jordan 6 sport blue...

For years, Britain had a spiffy trade deal with Iran regarding their prodigious oil fields. The...

Comment by authentic 2014 Nike Air Max Tailwind 2010 Grey Yellow Release Dates online

authentic 2014 Nike Air Max Tailwind 2010 Grey Yellow Release Dates online...

tvzvhxixp fzumodgdyg Nike Air Max Excellerate Dark Grey Blue December Releases 2014 bfbrsqijbt Nike Zoom Hyperfuse XDR White Grey Black for Kid wbqyglyx authentic cheap Nike Dunk High Pro SB Ferris Bueller Boulder Black free shipping for sale real Nike...

Comment by jordan outlet

jordan outlet...

Lou, I can't go along with you on this. If the majority of those 43 accidents was proven to have been caused by excessive speed, that's not the fault of the bridge. And I have a feeling the CHP estimate will be proven correct. Insurance companies rou...

Comment by jordans for sale

jordans for sale...

Recent Staff Blog PostsDeus ex machina east county bridgeTrail Mix: Rookie Guard Payton Looks Up To Damian LillardIt's Vancouver Brewfest time!Soccer analysis earns blogger some recognitionYoung athletes play hard, down play injuriesMaxPreps rates 10 ...

Comment by cheap christian louboutin Ankle Boots shoes store

cheap christian louboutin Ankle Boots shoes store...

Christian Louboutin Pensee 120mm Mary Jane Pumps Canari size 5 the best mens christian louboutin Sneakers shoes for sale Authentic Christian Louboutin Particule 100mm Pumps Leopard For Cheap sale pghmmhyf jzhbqbtwiwm ccmzvmplx buy authentic Christian L...

Comment by louis vuitton outlet store

louis vuitton outlet store...

the head in Saturday's 52 50 victory over Michigan State.Bush initiated a review...

Comment by authentic jordans

authentic jordans...

The revolutionary concept of online education, however, is much more than distance learning, it is empowerment.According to the United States Department of Education, online students perform better than their traditional, on campus, counterparts. The D...

Comment by Coach Wallets For Men

Coach Wallets For Men...

Welcome to online shop for all men’s and ladies’ , accessories and more at highly discounted prices. Browse all the collection here and get your favorite with free delivery and returns....

Comment by new jordan shoes

new jordan shoes...

Syriza head Alexis Tsipras campaigned on a platform for Greece to pull out of its commitments in the international bailout agreement, which has given the country billions in rescue loans. He has insisted he cannot back a government that will not overtu...

Comment by jordan 6 sport blue

jordan 6 sport blue...

Where To Buy Real premier finale 23 tdxnwvnqjvs Where Can I Buy Jordan 6 Carmine 2014 Restock Jordan retro yeezy 2 Where Can I Order Jordan 1 Big Size High Strap Yellow qgdrue www.martinsangusbeef.com/loginss.asp...

Comment by jordans for sale

jordans for sale...

representation, gather and form a group based on common interest and belief....

Comment by new jordans

new jordans...

If you're wondering how exactly that would work from a technological standpoint, Microsoft is breaking new ground there, too; the company is bringing together filmmakers and software engineers to create new interactive capabilities that include social...

Comment by cheap jordan shoes

cheap jordan shoes...

Did a really good job. Look at the cruddiness of the ceilings. I imagine that this time next year, Netflix and I will have gone separate ways. I think I'm worth more to it than Netflix is worth to me, and that Netflix matters less to me than it thinks...

Comment by louis vuitton outlet

louis vuitton outlet...

changement ne vient pas de Washington....

Comment by cheap jordans

cheap jordans...

Thank you for taking the time to offer your thoughts.The Miami Herald uses Facebook's commenting system. You need to log in with a Facebook account in order to comment.. Global News is celebrating one of its highest honours to date, accepting the pres...

Comment by jordans on sale

jordans on sale...

A growing number of studies have called that into question, however. The conclusions of the Brookings report are similar to a study funded by the oil industry and performed by the research firm IHS in May and a February paper by Resources for the Futur...

Comment by jordan boys shoes

jordan boys shoes...

Every now and then, I get an email from someone who's keen to get into science writing and wants to know how I started. Whenever I reply, and I always try to, I'm always left with the nagging feeling that my experience is but one of a multitude of ro...

Comment by cheap jordan shoes

cheap jordan shoes...

Just to dampen down the gay pride celebrations from this RA story about Stanhope wanting to legally recognise gay civil unions, the Federal AG Phil Ruddock has come out in this ABC report saying the Government will intervene unless the bill is changed ...

Comment by cheap jordan shoes

cheap jordan shoes...

They agreed to make Irena the topic of their team project. It was a decision that not only would attract national acclaim and worldwide attention but one that would take them half way around the world.. The Elite Eight is being held in Evansville, Ind....

Comment by jordan outlet store

jordan outlet store...

The asserted rights of slavery, which shock equality of all kinds, are cloaked by a fantastic claim of equality. If the slave States cannot enjoy what in mockery of the great fathers of the Republic, he misnames equality under the Constitution in other...

Comment by jordans

jordans...

Apparently after 10 minutes or so the children calm down, stop rampaging about and usually fall asleep. The effect appears to last for some time. If you want a lighter look for your wedding, choose this fantastic color combination. Watermelon dresses f...