473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 5885
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kenneth | last post by:
<list> seems to be a powerful structure to store the related nodes in memory for fast operations, but the examples I found are all related to primitive type storage. I'm doing a project on C++...
8
by: Ook | last post by:
This is my code in it's entireity: #include <iostream> using namespace std; template <typename T> class SortedList { public: friend ostream& operator<< (ostream& os, const SortedList<T>&...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
1
by: Michael Primeaux | last post by:
Why does the generic SortedList and generic SortedDictionary not define any virtual members? Thanks, Michael
4
by: Dave Booker | last post by:
So did the .NET 2.0 working group just run out of steam before it got to a ReadOnlyDictionary<> wrapper? I am publishing events containing a SortedList<>, and obviously I don't want event...
4
by: Christofer Dutz | last post by:
Hi, I am having a small problem, that is driving me nuts. My application reads some Xml and runs 2 Xsl Transformations to generate HTML. As soon as my second XSL introduces some <br/tags, the...
3
by: Beorne | last post by:
I have to retrieve the first kei I have inserted in a Dictionary<int,intcalled dict. The only method I found is: Dictionary<int,int>.KeyCollection.Enumerator dke = dict.Keys.GetEnumerator();...
2
by: Bruce Wood | last post by:
SortedList<Tgeneric has most of the methods of the old SortedList class, but it's missing a few, and one in particular that puzzles me. Anyone know why it has no GetByIndex method? It has...
4
by: jessy | last post by:
Thats my Question is it possible in Php to make the option items colored or to highlight some items.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.