Class within a Dictionary Q.

Hi,

I am trying to access my class that contains multiple properties inside of my dictionary. I can access my class properties just fine with array, but I just can’t seem to get the dictionary correctly.

This is what I am using, but I just get the actual “Class” itself and not the specific property of the class. The class that is inside of the CredentialGroupDictionary is called CredInfoClass btw. Below I am trying to extract a class property from CredInfoClass nested inside of CredentialGroupDictionary to then populate a new class called TargetDestinationClass1.Username.

  dim TargetDestinationClass1 as new TargetDestinationClass
  for i as integer = 0 to PreferencesWindow.CredentialGroupDictionary.Count -1
    TargetDestinationClass1.UserName = PreferencesWindow.CredentialGroupDictionary.Value( PreferencesWindow.CredentialGroupDictionary.Key(i))
  next

I tried nesting the CredInfoClass inside the Key and clearly that didn’t work for me. Any help would be very much appreciated.

Thanks!

dim c as CredInfoClass = CredentialGroupDictionary.Value( PreferencesWindow.CredentialGroupDictionary.Key(i) )
TargetDestinationClass1.Username = c.SomeProperty

BTW, if you are going to cycle through all the properties of your Dictionary anyway, it would be faster to do this:

dim values() as variant = PreferencesWindow.CredentialGroupDictionary.Values
for each v as Variant in values
  dim c as CreditInfoClass = v
  TargetDestinationClass1.Username = c.SomeProperty
next v

Kem thank you. It clicked for me after reading your example. I didn’t realize I had to extract all of the value variants first and then cycle through those. Thank you!!

If I understand your meaning, you don’t have to do that, it’s just more convenient. But you do have to cast the values, which are variants, to your class so you can access their properties.

Isn’t TargetDestinationClass1.Username overwritten on each iteration of the loop? Surely, if you’re copying over the values you want to preserve the keys as well?

Kem - I see what you are saying and I am still weak on casting so ill need to read up on that. I did like how you were bringing the dictionary values back into an array for processing so I was able to follow that. :slight_smile:

Andrew - Yes I will be adding the TargetDestinationClass1 to a new Dictionary and to keep my example pointed I left that out. Thanks again guys!

In a nutshell, a Variant can hold anything, so let’s say you have a class, MyClass, with a string property, MyString. If you create an instance of MyClass, MyClass1, you would access that property as MyClass1.MyString. Xojo knows what to do with that because it knows that MyClass1 is an instance of MyClass, and MyClass has that property.

But let’s say you assign MyClass1 to a Variant:

dim v as Variant = MyClass1

Now Xojo sees v as a container that can hold anything (or nothing), but doesn’t know what’s in it, so this will not work:

dim s as string = v.MyString // Error!

The reason this fails is because a Variant does not have a property called MyString, and Xojo doesn’t know what’s in v. “Casting” is just a way of telling Xojo, “treat this object as if it’s a ___”, like this:

if MyClass( v ).MyString = ... // This works

Or just assign the Variant to a variable that is defined as the proper class, as we did in the earlier code.

BTW, if your Variant contains an instance of MyClass, but you try to cast it as SomeDifferentClass, it will let you compile it (because it doesn’t know what’s in the Variant), but will throw an Exception when it tries to run that code.

Kem thank you for taking the time to explain casting. I really do appreciate it and I know others will as they google this. :slight_smile:

Thank you again.
Mike

(For the purpose of Internet documentation)

Xojo User Guide page 156 - Casting