The IF operator crash

Try the following code :

  dim dic as new Dictionary
  
  dic.value(1) = "one"
  dic.value(2) = "two"
  
  dim dic2 as new Dictionary
  
  for i as integer = 1 to 3
    dic2.value(i) = if( dic.HasKey(i), dic.value(i), "?")
  next
  
  dic.clear
  dic2.clear

The app will crash !

If you replace the line with the IF operator by a If statement (see below), it won’t crash :

    if dic.HasKey(i) then
       dic2.value(i) = dic.value(i)
    else
      dic.value(i) = "?"
    end if

Quite odd, huh ?!?

It seems that the IF operator doesn’t handle correctly the reference counts…

I can reproduce that. Let us know the Feedback number and I’ll add my crash report.

FYI, you should be using Lookup here.

 dim dic as new Dictionary
  
  dic.value(1) = "one"
  dic.value(2) = "two"
  
  dim dic2 as new Dictionary
  
  for i as integer = 1 to 3
    dic2.value(i) = dic.Lookup(i, "?")
  next
  
  dic.clear
  dic2.clear

Yes I know that but it is a simple code to illustrate the IF operator issue. Your code doesn’t crash.

Feedback number 33643

The problem would appear to be that the two types are different (variant vs. string). If you change it to:

   dic2.value(i) = if( dic.HasKey(i), dic.value(i).StringValue, "?")

then the code works.

Quite right. The compiler should probably stop that or compensate for it.

I added to the report, including a sample project. Martinho, just FYI: Feedback requests with sample projects get addressed faster.

@Kem; okay Thanks I’ll post a sample project the next time :wink:

@Jason, good point. But according to the documentation [quote]The return type is the common type between the two result values[/quote] and [quote]Having no common type results in a Type Mismatch compile error. [/quote].
So, no exception because the “common” type is Variant. I think there is something to be fixed…