473,396 Members | 2,003 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,396 software developers and data experts.

How to use this generic?

I have this class:

public class MyClass<TValue> where TValue : IMyClass, new( )
{...}

I need to create a class that will hold instances of the above class
using List<>:

public class MyClassCollection : List<View<TValue>>
{...}

The above will give this error:

The type or namespace name 'TValue' could not be found (are you missing
a using directive or an assembly reference?)

How can I do this?

Thanks,
Brett

Jun 26 '06 #1
15 1843
Brett,

If you do not specify TValue in the class declaration, then it assumes
that TValue is the name of the type, and not a type parameter. You want to
do:

public class MyClassCollection<TValue> : List<View<TValue>>
{}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...
I have this class:

public class MyClass<TValue> where TValue : IMyClass, new( )
{...}

I need to create a class that will hold instances of the above class
using List<>:

public class MyClassCollection : List<View<TValue>>
{...}

The above will give this error:

The type or namespace name 'TValue' could not be found (are you missing
a using directive or an assembly reference?)

How can I do this?

Thanks,
Brett

Jun 26 '06 #2

Nicholas Paldino [.NET/C# MVP] wrote:
Brett,

If you do not specify TValue in the class declaration, then it assumes
that TValue is the name of the type, and not a type parameter. You want to
do:

public class MyClassCollection<TValue> : List<View<TValue>>
{}

Hope this helps.


The problem with doing it that way means the collection will only hold
one type of MyClass. Let's say i do this:

MyClass<green> green = new MyClass<green>();
MyClass<red> red = new MyClass<red>();
MyClass<orange> orange = new MyClass<orange>();

Now I want to add each of them to the MyClassCollection. Using your
technique, it looks like this:

MyClassCollection<green> ColorCollection = new
MyClassCollection<green>();

I can't do MyClassCollection.Add(red) because it only takes types of
green. I want this collection to hold all three colors.

Thanks,
Brett

Jun 26 '06 #3
Brett,

Then you need to find a base class/interface that they all share, and
work with the lowest common denominator.

So say Green, Red, and Orange derive from Color. Your class would look
like:

public class MyClassCollection : List<View<Color>>
{}

And then you would have to cast to Red, Green and Orange for the
specific type functionality.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...

Nicholas Paldino [.NET/C# MVP] wrote:
Brett,

If you do not specify TValue in the class declaration, then it
assumes
that TValue is the name of the type, and not a type parameter. You want
to
do:

public class MyClassCollection<TValue> : List<View<TValue>>
{}

Hope this helps.


The problem with doing it that way means the collection will only hold
one type of MyClass. Let's say i do this:

MyClass<green> green = new MyClass<green>();
MyClass<red> red = new MyClass<red>();
MyClass<orange> orange = new MyClass<orange>();

Now I want to add each of them to the MyClassCollection. Using your
technique, it looks like this:

MyClassCollection<green> ColorCollection = new
MyClassCollection<green>();

I can't do MyClassCollection.Add(red) because it only takes types of
green. I want this collection to hold all three colors.

Thanks,
Brett

Jun 26 '06 #4

Nicholas Paldino [.NET/C# MVP] wrote:
Brett,

Then you need to find a base class/interface that they all share, and
work with the lowest common denominator.

So say Green, Red, and Orange derive from Color. Your class would look
like:

public class MyClassCollection : List<View<Color>>
{}

And then you would have to cast to Red, Green and Orange for the
specific type functionality.


Yes and I have an interface that does that. Say Color (from above) is
the interface name. I get this compile time error:

The type 'Color' must have a public parameterless constructor in order
to use it as parameter 'TValue' in the generic type or method
'MyClass<TValue>'

Is it possible to use only the interface (vs. base class) this way?

Thanks,
Brett

Jun 26 '06 #5
"Brett Romero" <ac*****@cygen.com> a écrit dans le message de news:
11**********************@y41g2000cwy.googlegroups. com...

|I have this class:
|
| public class MyClass<TValue> where TValue : IMyClass, new( )
| {...}
|
| I need to create a class that will hold instances of the above class
| using List<>:
|
| public class MyClassCollection : List<View<TValue>>
| {...}
|
| The above will give this error:
|
| The type or namespace name 'TValue' could not be found (are you missing
| a using directive or an assembly reference?)

As Nicholas said, you need to declare the type TValue before using it.

I have a question...

1. why are you deriving from a generic class, are you planning on extending
List<T> functionality ?

....and a suggestion ...

start out by declaring a non-generic class from which you will derive your
generic class, then you can have a list of the base class that will hold
different bound types.

public class MyClass
{
// non-generic stuff
}

public class MyClass<valueT> : MyClass
{
// generic stuff
}

then simply use a List<MyClass> as it is without deriving.

Of course, you will have to query each item to find its type and then cast
it to the appropriate type to use it.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 26 '06 #6
I have a question...

1. why are you deriving from a generic class, are you planning on extending
List<T> functionality ?


I'm not extending List<> functionality. What gives you that idea? I
simply want a typed list. I could choose to declare the List<> as
private in the class instead of inheriting it. Either way, I need it
typed.

Brett

Jun 26 '06 #7
"Brett Romero" <ac*****@cygen.com> a écrit dans le message de news:
11*********************@b68g2000cwa.googlegroups.c om...

| I'm not extending List<> functionality. What gives you that idea? I
| simply want a typed list. I could choose to declare the List<> as
| private in the class instead of inheriting it. Either way, I need it
| typed.

By binding a List<T> to a given type, you are creating a "typed list".
Genreic lists are strongly typed as soon as they are bound.

List<Customer> customerList = new List<Customer>();

now customerList is a typesafe list that can only accept and return
Customers.

You don't need to derive from it or do anything else.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 26 '06 #8

Joanna Carter [TeamB] wrote:
"Brett Romero" <ac*****@cygen.com> a écrit dans le message de news:
11*********************@b68g2000cwa.googlegroups.c om...

| I'm not extending List<> functionality. What gives you that idea? I
| simply want a typed list. I could choose to declare the List<> as
| private in the class instead of inheriting it. Either way, I need it
| typed.

By binding a List<T> to a given type, you are creating a "typed list".
Genreic lists are strongly typed as soon as they are bound.

List<Customer> customerList = new List<Customer>();

now customerList is a typesafe list that can only accept and return
Customers.

You don't need to derive from it or do anything else.


Let me clarify a little more. I want to call MyClass.Add() vs
MyClass.List.Add(). If I don't inherit, the List's Add, Remove, Count,
etc need to be encapsulated. This means I to have create all of those
methods. Inheritance avoids that.

Brett

Jun 26 '06 #9
Brett Romero <ac*****@cygen.com> wrote:
You don't need to derive from it or do anything else.


Let me clarify a little more. I want to call MyClass.Add() vs
MyClass.List.Add(). If I don't inherit, the List's Add, Remove, Count,
etc need to be encapsulated. This means I to have create all of those
methods. Inheritance avoids that.


At the expense of making people think that you're trying to specialise
the behaviour of lists - because that's what inheritance is for.

See http://msmvps.com/jon.skeet/archive/...itancetax.aspx
for my view on the risks and overuse of inheritance.

--
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
Jun 27 '06 #10
"Brett Romero" <ac*****@cygen.com> a écrit dans le message de news:
11**********************@75g2000cwc.googlegroups.c om...

Let me clarify a little more. I want to call MyClass.Add() vs
MyClass.List.Add(). If I don't inherit, the List's Add, Remove, Count,
etc need to be encapsulated. This means I to have create all of those
methods. Inheritance avoids that.

As Jon says, you are really overusing inheritance here.

Assuming you want to include an internal list in a class, then you don't
want to go to the expense of inheriting a generic list just to include the
derived class, which is identical in behaviour to the class from which it
derives.

I take it you want something like this

publlic class Employee
{
...
}

public class Company
{
private string name;

private Address address;

...

private List<Employee> employees = new List<Employee>();

// public properties, etc
}

This then means that you would call :

{
aCompany.Employees.Add(...)
}

Which makes more sense than deriving Company from List<Employee>, as a
Company could also have other lists of things like Customers, etc.

This way of allowing access to the list itself, is known as Aggregation and
it assumes that the list of objects is not "owned" by the containing object.
IOW, the people that we can call Employees are not created by the Company,
they have a life outside of the Company.

OTOH, if you want to model something like a SalesOrder, then you would
encapsulate the list, hiding its own methods, thus preventing unauthorised
use of the list.

public class ReadOnlyList<T> : IEnumerable<T>
{
public T this[int index] { get {...} }

public int Count { get {...} }

IEnumerator<T> IEnumerable<T>.GetEnumerator() { get {...} }

private List<T> list;

public ReadOnlyList(List<T> list)
{
this.list = list;
}
}

This is a readonly list wrapper class that allows examination of the items
in a list but not manipulation of the list itself. It is a wrapper class.

public class SalesOrder
{
public class Line
{
...

internal Line() // only accessible in business class assembly
{
...
}
}

private List<Line> lines = new List<Line>();

public ReadOnlyList<Line> Lines
{
get { return new ReadOnlyList<Line>(lines); }
}

public Line AddLine()
{
Line result = new Line();
lines.Add(result);
return result;
}
}

Now the Lines property returns a read-only list for examination and
alteration of the items only.

Adding a Line is only possible by asking the SalesOrder to create one and
then return it for editing.

This way of preventing list operations whilst allowing operations on the
items, is known as Composition.

Does that help or confuse ? :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 27 '06 #11
"Brett Romero" <ac*****@cygen.com> wrote:

[...]

To add to what the others wrote:

List<T> isn't really meant for inheritance. Most of its methods are
static, and so can't be overridden and still work polymorphically as a
List<T>.

Collection<T> is a much better bet if you want to descend from a
collection class and still have strong typing.

-- Barry

--
http://barrkel.blogspot.com/
Jun 27 '06 #12
The thread is veering off the point. We're getting distracted about
why List<T> shouldn't be inherited in this case. I agree it shouldn't.
I burn the base class fairly quick for little gain. That aside...

Now, this still doesn't get me anywhere with the original problem.
Which is, How do I handle List<MyClass<TValue>> in the collection
class?

Thanks,
Brett

Jun 27 '06 #13
To reiterate, I'd like to do something like this:

MyClass<green> green = new MyClass<green>();
MyClass<red> red = new MyClass<red>();
MyClass<orange> orange = new MyClass<orange>();

MyClassCollection myColl = new MyClassCollection();
myColl.Add( green );
myColl.Add( red );
MyColl.Add( orange );

MyClass, MyClassCollection and all of the specific color classes (i.e.
green, red, orange) implement IMyClass. Isn't there a way for me to
exploit this so I can get that common type into the collection's
List<MyClass<IMyClass>> (or how ever it should be)?

Brett

Jun 27 '06 #14
"Brett Romero" <ac*****@cygen.com> a écrit dans le message de news:
11*********************@u72g2000cwu.googlegroups.c om...

| To reiterate, I'd like to do something like this:
|
| MyClass<green> green = new MyClass<green>();
| MyClass<red> red = new MyClass<red>();
| MyClass<orange> orange = new MyClass<orange>();
|
| MyClassCollection myColl = new MyClassCollection();
| myColl.Add( green );
| myColl.Add( red );
| MyColl.Add( orange );
|
| MyClass, MyClassCollection and all of the specific color classes (i.e.
| green, red, orange) implement IMyClass. Isn't there a way for me to
| exploit this so I can get that common type into the collection's
| List<MyClass<IMyClass>> (or how ever it should be)?

Why would MyClassCollection implement IMyClass ???

What do you mean: all your specific color classes implement IMyClass, why
would they do that ???

As long as MyClass<T> implements the non-generic IMyClass interface, then
you can simply use List<IMyClass> to hold instances of any bound MyClass<T>
type.

public interface IMyClass
{
...
}

public class Green
{
...
}

public class Red
{
...
}

public class Orange
{
...
}

public class MyClass<T> : IMyClass
{
...
}

public class MyClassCollection : List<IMyClass>
{
...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 27 '06 #15
I have this doing what I need but the approach turned out to be very
different.

Thanks for the input,
Brett

Jun 27 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
3
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
15
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a...
2
by: ADN | last post by:
Hi, I have a method which calls my service factory: class person { public int ID { get; set: } public string Name {get; set;} } IList<Personmypeople = __serviceFactory.Fetch(new Person())
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.