471,319 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Why can't I use SortedList<T, T>?

I'd like to type a SortedList as very generic on class instantiation:
SortedList<T, T> _sl;

Then in two particular methods of the class that is called first(of
which only one or the other is used per instance), I will do

_sl = new SortedList<string,int>();

or

_sl = new SortedList<string,string>();

but I always get this error:
The type or namespace name 'T' could not be found (are you missing a
using directive or an assembly reference?)

Collections.Generic is included. Why is the above error being thrown?
Must I declare both types of list to get around this?

Thanks,
Brett

Feb 10 '06 #1
5 5809
Brett Romero <ac*****@cygen.com> wrote:
I'd like to type a SortedList as very generic on class instantiation:
SortedList<T, T> _sl;

Then in two particular methods of the class that is called first(of
which only one or the other is used per instance), I will do

_sl = new SortedList<string,int>();

or

_sl = new SortedList<string,string>();

but I always get this error:
The type or namespace name 'T' could not be found (are you missing a
using directive or an assembly reference?)

Collections.Generic is included. Why is the above error being thrown?
Must I declare both types of list to get around this?


You can't decide at *run-time* what the types will be - the point of
generics is to make that a *compile-time* decision.

It sounds like you want your containing class to be generic too:

class Foo<T>
{
SortedList<string,T> s1;
}

Then you'd create either a Foo<int> or a Foo<string>.

However, if you've got "two methods, of which only one or the other is
used per instance" that sounds like you should actually have two
different types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 10 '06 #2
I'm doing the same thing with my list. One list compiles as <string,
int> and the other as <string, string>. How is that different than
what you have mentioned?

Thanks,
Brett

Feb 10 '06 #3
Brett Romero <ac*****@cygen.com> wrote:
I'm doing the same thing with my list. One list compiles as <string,
int> and the other as <string, string>. How is that different than
what you have mentioned?


In my code, everything is fixed at compile-time. In your code, you're
trying to delay the decision to run-time.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 10 '06 #4
How so? In your code, you say to create either Foo<int> or a
Foo<string>. Isn't the decision to use either int or string being
delayed to run time? That's what I'm doing. I also start with "T" and
at some point it becomes either int or string, just as yours does.

Thanks,
Brett

Feb 10 '06 #5
Brett Romero <ac*****@cygen.com> wrote:
How so? In your code, you say to create either Foo<int> or a
Foo<string>. Isn't the decision to use either int or string being
delayed to run time?
No. The compiler knows when it's creating Foo<int> or Foo<string>, and
you wouldn't be able to have a field which was Foo<T> unless T itself
was a type parameter (for the type or the method).
That's what I'm doing. I also start with "T" and
at some point it becomes either int or string, just as yours does.


I suggest you try writing a small program with each way of doing
things, and you should see the difference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ook | last post: by
1 post views Thread by Michael Primeaux | last post: by
4 posts views Thread by Dave Booker | last post: by
4 posts views Thread by Christofer Dutz | last post: by
3 posts views Thread by Beorne | last post: by
2 posts views Thread by Bruce Wood | last post: by
4 posts views Thread by jessy | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.