SortedDictionary<string, XYZ> to SortedDictionary<string, object>

Hello,

I have in C#, ASP.NET 3.5 the following code

public class ABC{
public class XYZ {
  some implementation
}
}

Then I have a method that takes an arg of type SortedDictionary<string, object>. When I try to pass in SortedDictionary<string, ABC.XYZ>, I get the following compile error:
'System.Collections.Generic.SortedDictionary<string,ABC.XYZ>' to 'System.Collections.Generic.SortedDictionary<string,object>'      

Why? ABC.XYZ is of type object, isn't it? Any idea how to go around this in a clever way?
bovlkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_Katka_Commented:
Hi, I would try to make the method generic as in

public void MyMethod<TCollectionType>(TCollectionType collection) { }

And then call it with MyMethod<SortedDictionary<x, y>>(dictionary).

regards,
Kate
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_Katka_Commented:
Or a bit more clever you can do it by

public void MyMethod<TKey, TValue>(SortedDictionary<TKey, TValue> collection) { }

regards,
Kate
0
bovlkAuthor Commented:
Still a question: why does not the SortedDictionary work?
0
bovlkAuthor Commented:
BTW, it was even simpler
public void MyMethod<T>(string s, int i, SortedDictionary<string, T> values)
{
}
works amazingly well.
0
_Katka_Commented:
You're welcome. :)

To explain to you. Generics have to of exactly the same type.

http://stackoverflow.com/questions/1431636/htmlhelperchildtype-not-assignable-to-htmlhelpermothertype

By moving them to single parameters the check is not:

SortedDictionary<x, y> vs SortedDictionary<x, z>

but rather

x vs x => ok
y vs z => when z is derived from y => ok

regards,
Kate
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.