|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
IIF in vb.net acts weird.. or is it meHi all,
why in the world would this fail ( throw an exception) Dim str As String = "" MsgBox(IIf(IsNumeric(str), Convert.ToInt32(str).ToString, str)) Why does it try to evaluate the true part when the condition is false. Or am i missing something here. Thanks in advance. "parez" <psaw***@gmail.com> schrieb: Because it's a function.> why in the world would this fail ( throw an exception) > > > Dim str As String = "" > > MsgBox(IIf(IsNumeric(str), Convert.ToInt32(str).ToString, str)) > > Why does it try to evaluate the true part when the condition is false. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> I thought it was more like a ?: operator.. I forgot it was a
function.. Thanks again Parez,
Not only however mainly because its behaviour is mostly advices to avoid the IIF in VBNet. Cor Parez,
As Cor suggests a number of developers discourage its use, as it does evaluate both of its parameters, which may have undesired side effects as you found out. Plus it is not Option Strict On friendly, as it accepts Object as its parameters & returns an Object. Accepting & returning Object means that it will box value parameters, which is a slight performance hit & causes extra pressure on the GC. Returning object means you need to cast to the specific type, leading to "awkward" code. In places where it does make sense to use the IIf function, I normally use my Generic IIf function. Being Generic removes the "boxing penalty" and the "awkward" code factor. However its still a function ;-) http://www.tsbradley.net/Cookbook/Generics/genericIIf.aspx -- Show quoteHide quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "parez" <psaw***@gmail.com> wrote in message news:1141079568.816497.182130@e56g2000cwe.googlegroups.com... |I thought it was more like a ?: operator.. I forgot it was a | function.. | Thanks again | It's similiar to If xxx and yyy then...both are evaluated whereas in If xxx
andalso yyy then... only the first xxx is evaluated if it's false...yyy is only evaluated if xxx is true. If you pass parameters to a function, all are evaluated to their final value. -- Show quoteHide quoteDennis in Houston "parez" wrote: > I meant it returns str. but it also tries to evaluate the true part > >
Upgrading VB6 to .Net
Distributing VB 2005 projects OO & Interface question function to convert string to 1 dimensional array of long button in bold AcroRd32.exe left open System.IO.Ports.SerialPort.GetPortNames return wrog names above CO Getting primary key from new record instered into one table using ADO.net Retrieving a handle to a private delegate Detecting a dialog |
|||||||||||||||||||||||