Dictionary.HasKey NilObjectException

Greetings,

I have the following code :

[code]Dim test As New Xojo.Core.Dictionary

test.Value(“demo”) = “1”

If test.HasKey(“demo1”) Then
'Do something
Else
'Do something else
End If[/code]

So theoretically the key does not exist so it should be false , in my case it crashes the app with a NilObjectException stopping to the line If test.HasKey("demo1") Then

So from what i understood the idea of HasKey is to search if the key exists or not and according to the docs it says

[code]HasKey(key As Auto) As Boolean
Determines where or not the Dictionary contains a value for the specified key.

Parameters
key An Auto that identifies the key to look for.
Return Value
True if the key exists in the Dictionary, False if it does not.[/code]

But in my case it throws an Exception, is that normal ? or it`s another bug ?

Is this really the code you are using? Works for me.
The NiException looks like test has gone out of scope. Do you declare it elsewhere and it’s not available when you check for it?

[quote=250438:@Ulrich Bogun]Is this really the code you are using? Works for me.
The NiException looks like test has gone out of scope. Do you declare it elsewhere and it’s not available when you check for it?[/quote]

Well indeed you are right , i have a property in a container control that im calling so theoretically it should be enabled over the containers or maybe im doing it wrong.

I have in containercontrol1 a property called test and set as Xojo.Core.Dictionary , then in the container control i initiate the dictionary with Dim test As New Xojo.Core.Dictionary.

again in the container control im populating the dictionary and in the main window im trying to see if i have the Key or not.

So normally how i initiate a global value that is set as a dictionary ?

The idea is to populate it on init then to do the processing and once the form is closed to clear the data .

Maybe i`m doing it wrong.

If you have a property Test and then issue dim test as new Something, you are creating a local variable with the same name as the property and it will go out of scope when the method ends. Instead, do something like this:

Test = new Something

or, to keep from replacing it…

if Test is nil then
  Test = new Dictionary
end if

Even better, create a computed property that uses a shadow property, mTest, that does this work for you.

Property Test Getter
  if mTest is nil then
    mTest = new Something
  end if
  return mTest
End Getter

[quote=250441:@Kem Tekinay]If you have a property Test and then issue dim test as new Something, you are creating a local variable with the same name as the property and it will go out of scope when the method ends. Instead, do something like this:

Test = new Something

or, to keep from replacing it…

if Test is nil then
  Test = new Dictionary
end if

Even better, create a computed property that uses a shadow property, mTest, that does this work for you.

Property Test Getter if mTest is nil then mTest = new Something end if return mTest End Getter [/quote]

Thanks Kem, it worked .