Dictionary not retaining keys/values

I’ve defined a private dictionary property which is populated via a recordset in a one of my methods.

Property definition:

Private assoc_env As dictionary

Initialized in the window’s Open event handler:

assoc_env = new Dictionary

Dictionary assignments inside the get_assoc_env() method

[code]
if position_param.RecordCount = 1 then

    assoc_env.Value("server") = position_param.Field("server")
    assoc_env.Value("share") = position_param.Field("share")
    assoc_env.Value("G") = position_param.Field("pos_group")
    assoc_env.Value("D") = position_param.Field("pos_dept")
    assoc_env.Value("N") = position_param.Field("pos_node")
    assoc_env.Value("script") = position_param.Field("script")
    
    System.DebugLog assoc_env.Value("server")
    System.DebugLog assoc_env.Value("share")
    System.DebugLog assoc_env.Value("G")
    System.DebugLog assoc_env.Value("D")
    System.DebugLog assoc_env.Value("N")
    System.DebugLog assoc_env.Value("script")
    
  end if[/code]

The debugLog entries have the expected values.

After calling the method, I then output the values again to the DebugLog.

[code] if App.store = “099” then
StatusWindow.AppendText “Preparing to retrieve associate environment settings.” + EndOfLine + EndOfLine

        get_assoc_env(PositionId)
        
        System.DebugLog assoc_env.Value("server")
        System.DebugLog assoc_env.Value("share")
        System.DebugLog assoc_env.Value("G")
        System.DebugLog assoc_env.Value("D")
        System.DebugLog assoc_env.Value("N")
        System.DebugLog assoc_env.Value("script")
        
        MsgBox "quiting"
        quit
        
      end if[/code]

However, when I run it, it crashes with a “Debughlogin.exe has stopped working” window.

I added a breakpoint at the start of the DebugLog calls outside of the method and stepped thru it and inspected the status of the dictionary. I found that it had 6 keys, which is correct, but all keys and values were Nil.

What would cause it to loose the keys/values but retain the correct key count?

Would it be better to have the method return the dictionary instead of assigning the way I have done? If so, what is the proper syntax because so far my attempts have failed with the same crash as described above.

I’ve never seen anything like this. I suspect you’re really overwriting the contents elsewhere and just haven’t found it.

You’ll need to tell us on what line it’s crashing.

Again, I suspect the problem lies elsewhere but stepping through code might yield some good clues.

Did you also include a line like

dim assoc_env as Dictionary

somewhere in your code? If so, you might think you’re using the property, but you’re actually using a local variable that will disappear as soon as the method ends.

Bob, as it is now it is crashing on the first system.debuglog call outside/after the method call.

I’ll need to readjust the code abit to retest having the method return the dictionary to verify the exact point where it crashes but I believe it was at the same point.

So far I’ve focused the debugging on this small section where it’s crashing. I’ll try stepping through from the beginning to see if that sheds any more light on the issue.

Kem, I do not have that dim statement anywhere in my code.

One factor which might come into play is that I’m using an older version i.e., RB2008r2

I’d like to upgrade, but management doesn’t feel it’s required so they won’t approve the expenditure.

[quote=176598:@Ron Bergin]I’ve defined a private dictionary property which is populated via a recordset in a one of my methods.
assoc_env.Value(“server”) = position_param.Field(“server”)
assoc_env.Value(“share”) = position_param.Field(“share”)
assoc_env.Value(“G”) = position_param.Field(“pos_group”)
[/quote]
Retaining the database FIELDS will not work as once the record set closes those will be inaccessible
Retain the values from those records

One of the pitfalls of working with Variants.

That’s a pretty odd limitation. Is there anyway to get around that limitation? I would really like to retain those values after closing the db connection.

Well, the argument is that you’ll get better support for issues like this. :wink:

I agree, and will make another attempt to convince them.

Nothing stops you from storing the values. Instead, you are storing the object that stores the values, so change code like this:

        assoc_env.Value("server") = position_param.Field("server")

to

        assoc_env.Value("server") = position_param.Field("server").StringValue

Perfect, that’s exactly what I was missing/needed.

Sorry to say that I don’t use RB enough to see what should have been obvious.

Thank You

Not odd at all.
The db record set is not necessarily all in memory and when you close the connection or recordset that memory is invalid.
Anything that depended on the record set, like a field in the recordset, is also then invalid.

Yes, that part is clear/obvious but, since I copied what I thought was the values in the recordset to the dictionary, I was expecting those values to be retained in the dictionary. As Kem pointed out, I was copying (or maybe aliasing) the object instead of the value as I thought I was doing.