Strong Typed Collections

F

farseer

I'd like my collections to contain specific types of objects. Most of
the topics i read on this involves creating a custom collection class
that extends DictionaryBase or IEnumerable. But this means for every
object type i care about, i must create a custom collection class for.
Isn't there a simpler way to say "i want this collection to contain
only objects of Class X"?

thanks
 
F

farseer

ok...i see that i can use system.collections.generic namespace for
this. in my case, i want to use a key/value pair so IDictionary< int,
MyClass > is what i use. The problem is, how do i cast a hashtable to
this?
If that can't be done, how can i instantiate and add values to a
collection declared as IDictionary< int, MyCalss>.

Also, is there a way to declare a type or alias for "IDictionary<int,
MyClass>", so i don't have to type that declaration everytime i need to
declare a variable of that particular collection?
 
A

Andreas Huber

farseer said:
ok...i see that i can use system.collections.generic namespace for
this. in my case, i want to use a key/value pair so IDictionary< int,
MyClass > is what i use. The problem is, how do i cast a hashtable to
this?

You don't. You'd rather use Dictionary, which is the strongly typed
variant of Hashtable.
Also, is there a way to declare a type or alias for "IDictionary<int,
MyClass>", so i don't have to type that declaration everytime i need
to declare a variable of that particular collection?

At namespace scope (I hope I get the syntax right):

using IMyClassDictionary = IDictionary<int,MyClass>;

Regards,
 
F

farseer

btw, can that "using" statement be declared as a type under a
namespace. so that if that namespace is used in other classes, i can
simply refer to "IMyClassDictionary" instead of having to remember to
declare the alias again?
 
K

Kevin Spencer

There is a System.Collections.Generic.Dictionary<TKey,TItme> class that you
can use. If you don't want to re-declare it every time, just inherit it:

class MyDictionary : System.Collections.Generic.Distionary<int, MyClass>
{
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
F

farseer

Thanks...
Dictionary is what i used. I have a follow up issue...the Values
property of the dictionary returns an ICollection<T>. This does not
Type does support a "contains" method like the IList. Is there a
reason why?
If wanted to return an IList, how can i do that? Would i need to write
my own generic class for this?
 
F

farseer

Thanks. This Dictionary object's Value property returns an
ICollection<T> that does not support a "contains" method. Is there any
object that i can use that supports a "contains" method? I know the
the List<T> object does, but i can't cast to that it seems.

Lastly, it does not appear as though i can use this ICollection<T> as a
source for a ListBox. I have code that uses an array of MyObjects as
the source fo a list box. i set the displayMember of that listbox and
all is fine. However once i change the source for that listbox from
the array of MyObjects to an ICollection< MyObject >, it no longer
works.
 
K

Kevin Spencer

It really helps to quote the message you're replying to! Less work for
everybody else. ;-)
This does not
Type does support a "contains" method like the IList. Is there a
reason why?

Of course there is. However, I don't know what that reason is, offhand. Do
you really need to know why? If you spend enough hours studying the .Net
Framework SDK, and Googling, eventually you may figure it out. But it won't
bring your project any closer to completion. On the other hand, you will
have learned a great deal about the .Net platform and the CLR, so it won't
have been entirely wasted! ;-)

If you want to implement a "Contains" method for it, just write one.
Example:

class MyDictionary : System.Collections.Generic.Distionary<int, MyClass>
{
public bool Contains(MyClass instance)
{
foreach (MyClass c in Values)
if (c.Equals(instance)) return true;
return false;
}
}

Note that you would have to implement your own Equals override for MyClass
if you don't want to specify that the 2 references point to the same class
instance.
If wanted to return an IList, how can i do that?

IList is an interface. It is implemented in quite a few types. What would
you want to return one from? A method or a property?

This is your original question:
If that can't be done, how can i instantiate and add values to a
collection declared as IDictionary< int, MyCalss>.

Also, is there a way to declare a type or alias for "IDictionary<int,
MyClass>", so i don't have to type that declaration everytime i need to
declare a variable of that particular collection?

I presumed you wanted to implement a strongly-typed Dictionary class, and
that you wanted to avoid having to pass a type parameter to it whenever you
used it. My answer was:

class MyDictionary : System.Collections.Generic.Distionary<int, MyClass>
{
}

This class inherits a strongly-typed Generic Dictionary class with an
integer type Key, and a MyClass type Value. If you wanted an IList, why
didn't you say so? Or, are you just asking out of curiosity? In any case,
the answer is, if you want to write a method that returns an IList of some
kind or other, write one.
Would i need to write
my own generic class for this?

Are you sure you know what a Generic class is? An IList is not a generic
type. Why would you need to write a Generic class to create a method that
returns a specific type?

Maybe I'm just not understanding what it is you need. Perhaps if you can be
more specific, I can help.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
F

farseer

i wanted to return an IList because i would like listboxes to be able
to to use it as a datasource. Listboxes only support IList and List
types/interfaces i think (well, that is what the exception i was
getting when trying to use ICollection states anyway). What i ended up
doing was to convert by simply instantiating a new List<> as passing
the ICollection<> to the constructor. this works fine.
 

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