0

I use simple if statement and would like to retrieve true or false as Boolean type. I did like this:

Iif(text = "something", Convert.ToBoolean(True), Convert.ToBoolean(False))

I was trying also this:

Iif(text = "something", True, False)

For both cases i receive false, but my expression is for sure true. What i am doing wrong?

Arie
  • 3,041
  • 7
  • 32
  • 63
  • http://stackoverflow.com/questions/28377/iif-vs-if – Steve Jul 17 '15 at 10:46
  • 2
    Use `If` operator instead of old `IIf` function. The former is a (strongly typed) short circuiting operator which evaluates the second condition only if the first was `False`, the latter evaluates both always. – Tim Schmelter Jul 17 '15 at 10:46
  • You should also show us the value of `text`, maybe it is `Something` or has leading/trailing spaces. – Tim Schmelter Jul 17 '15 at 10:49

4 Answers4

1

Use the If operator.

    Dim someString As String = "something"
    Dim matchSomeString As Boolean = If(someString = "something", True, False)
dbasnett
  • 11,334
  • 2
  • 25
  • 33
1

For a simple Boolean value, you can use the comparison result directly

dim text as String = "something"
dim result as Boolean

result = (text ="something")
Pablo Fébolo
  • 106
  • 1
  • 4
0

AS by the MSDN definition

IIF : Returns one of two objects, depending on the evaluation of an expression.

IIF function has a return value that is you need to store the True or false part to some variable.

For eg:

Dim _decision As Boolean  =IIf(text = "something", True, False);

then convert as

Convert.ToBoolean(_decision );
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
0

@Tushar Gupta (sorry can't comment yet, but I was about to answer any way)

You don't need to convert your value i juste tested the code below and it perfectly worked (irt was 4 and not 2 at the end of the run)

Dim irt = 2
Dim text As String = "abc"
Dim o = IIf(text = "abc", True, False)
If o Then irt = 4
End

So I guess you have not stored your return value, or your text value has not exactly the value you expect.

Anyway if you need only true/false I don't see why you are not using a simple if who would be faster.

Jusanne
  • 115
  • 11