Generics Conversion

W

Wonko the Sane

Hi All,

I’m sure it’s because my brain is fried, but I can’t figure something
(seemingly simple) out.

I have created a set of classes to “simplify†a set of things that I am
working on. This is currently the entire class:

internal class MySettingsDictionary : Dictionary<string, UserDefinedClass>
{
}

I also have a function in another class that returns something of the same
Dictionary type:

public static Dictionary<string, UserDefinedClass>
GetSettingsDictionary()
{
return mSettings;
}

The question is, is there a simple way to convert the return of the function
to the type?

At runtime, this returns null:
MySettingsDictionary settings = ClassA.GetSettingsDictionary as
MySettingsDictionary;

and this throws an InvalidCastException:
MySettingsDictionary settings = (MySettingsDictionary)
AClass.GetSettingsDictionary();

Thanks,
WtS
 
W

Wonko the Sane

Mark Rae said:
Does this work?

MySettingsDictionary<string, UserDefinedClass> settings =
AClass.GetSettingsDictionary();

No - that is a compiler error ("The non-generic type 'MySettingsDictionary'
cannot be used with type arguments").

Not sure if you meant this, but this correctly returns the data I am
expecting:
Dictionary<string, UserDefinedClass> settings =
AClass.GetSettingsDictionary();

but then I have to set each key and value from the KeyValuePair enumeration,
which really defeats the purpose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top