PC Review


Reply
Thread Tools Rate Thread

Collection of Generic Objects

 
 
msnews.microsoft.com
Guest
Posts: n/a
 
      2nd Dec 2005
I got (what I hope to be is) a simple question....

I have a class called PropertyObject<T>

so in my code I created something like this..

name = new PropertyObject<string>("goot");
age = new PropertyObject<int>(20);

but what I would rather do is this

List<PropertyObject<T>> props = new List<PropertyObject>();
props.Add(new PropertyObject<string>("goot"));
props.Add(new PropertyObject<int>(20));

where the List<> props can only contain PropertyObject<T>'s of any T

Is this posible or does List<> have to contain only PropertyObjects of the
same generic type parameter and in which case I have to use an ArrayList?


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      2nd Dec 2005
A Generic class is only un-typed until you use it. It is the .Net equivalent
of a C++ Template, which can be used to create a variety of strongly-typed
classes having similar characteristics and/or behavior. Once you create an
instance of the class, you must pass a type parameter to it. At compile-time
it is strongly-typed. A Generic class, therefore is not like a base class,
or an Object, or a Variant. It cannot be used without passing a type
parameter to it. And once that type parameter is passed, it is set in stone.
For example, if you create a new PropertyObject<string>, how do you convert
it to a PropertyObject<int>? You don't. You can't.

In other words, what you're describing is not possible.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition

"msnews.microsoft.com" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I got (what I hope to be is) a simple question....
>
> I have a class called PropertyObject<T>
>
> so in my code I created something like this..
>
> name = new PropertyObject<string>("goot");
> age = new PropertyObject<int>(20);
>
> but what I would rather do is this
>
> List<PropertyObject<T>> props = new List<PropertyObject>();
> props.Add(new PropertyObject<string>("goot"));
> props.Add(new PropertyObject<int>(20));
>
> where the List<> props can only contain PropertyObject<T>'s of any T
>
> Is this posible or does List<> have to contain only PropertyObjects of the
> same generic type parameter and in which case I have to use an ArrayList?
>



 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      2nd Dec 2005
> In other words, what you're describing is not possible.

that's right.

however if you just look for a collection and are not aware of your options,
you could use:
System.Collection.ArrayList
or
System.Collection.Generic.List<object>

>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> If you push something hard enough,
> it will fall over.
> - Fudd's First Law of Opposition
>
> "msnews.microsoft.com" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I got (what I hope to be is) a simple question....
>>
>> I have a class called PropertyObject<T>
>>
>> so in my code I created something like this..
>>
>> name = new PropertyObject<string>("goot");
>> age = new PropertyObject<int>(20);
>>
>> but what I would rather do is this
>>
>> List<PropertyObject<T>> props = new List<PropertyObject>();
>> props.Add(new PropertyObject<string>("goot"));
>> props.Add(new PropertyObject<int>(20));
>>
>> where the List<> props can only contain PropertyObject<T>'s of any T
>>
>> Is this posible or does List<> have to contain only PropertyObjects of
>> the same generic type parameter and in which case I have to use an
>> ArrayList?
>>

>
>



 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      2nd Dec 2005
Thanks Kevin,
Something in my head told me to think I could that because after all
they are all PropertyObjects...

Being new to something sometimes makes you reach for things that aren't
really there

-Scott

"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>A Generic class is only un-typed until you use it. It is the .Net
>equivalent of a C++ Template, which can be used to create a variety of
>strongly-typed classes having similar characteristics and/or behavior. Once
>you create an instance of the class, you must pass a type parameter to it.
>At compile-time it is strongly-typed. A Generic class, therefore is not
>like a base class, or an Object, or a Variant. It cannot be used without
>passing a type parameter to it. And once that type parameter is passed, it
>is set in stone. For example, if you create a new PropertyObject<string>,
>how do you convert it to a PropertyObject<int>? You don't. You can't.
>
> In other words, what you're describing is not possible.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> If you push something hard enough,
> it will fall over.
> - Fudd's First Law of Opposition
>
> "msnews.microsoft.com" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I got (what I hope to be is) a simple question....
>>
>> I have a class called PropertyObject<T>
>>
>> so in my code I created something like this..
>>
>> name = new PropertyObject<string>("goot");
>> age = new PropertyObject<int>(20);
>>
>> but what I would rather do is this
>>
>> List<PropertyObject<T>> props = new List<PropertyObject>();
>> props.Add(new PropertyObject<string>("goot"));
>> props.Add(new PropertyObject<int>(20));
>>
>> where the List<> props can only contain PropertyObject<T>'s of any T
>>
>> Is this posible or does List<> have to contain only PropertyObjects of
>> the same generic type parameter and in which case I have to use an
>> ArrayList?
>>

>
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      2nd Dec 2005
I understand perfectly Scott. Generics took me awhile to get my head around
as well. I just spent a long time doing it until it all gelled.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a horse to water,
but you can't make him think.

"Scott" <-> wrote in message news:(E-Mail Removed)...
> Thanks Kevin,
> Something in my head told me to think I could that because after all
> they are all PropertyObjects...
>
> Being new to something sometimes makes you reach for things that aren't
> really there
>
> -Scott
>
> "Kevin Spencer" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>A Generic class is only un-typed until you use it. It is the .Net
>>equivalent of a C++ Template, which can be used to create a variety of
>>strongly-typed classes having similar characteristics and/or behavior.
>>Once you create an instance of the class, you must pass a type parameter
>>to it. At compile-time it is strongly-typed. A Generic class, therefore is
>>not like a base class, or an Object, or a Variant. It cannot be used
>>without passing a type parameter to it. And once that type parameter is
>>passed, it is set in stone. For example, if you create a new
>>PropertyObject<string>, how do you convert it to a PropertyObject<int>?
>>You don't. You can't.
>>
>> In other words, what you're describing is not possible.
>>
>> --
>> HTH,
>>
>> Kevin Spencer
>> Microsoft MVP
>> .Net Developer
>> If you push something hard enough,
>> it will fall over.
>> - Fudd's First Law of Opposition
>>
>> "msnews.microsoft.com" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>I got (what I hope to be is) a simple question....
>>>
>>> I have a class called PropertyObject<T>
>>>
>>> so in my code I created something like this..
>>>
>>> name = new PropertyObject<string>("goot");
>>> age = new PropertyObject<int>(20);
>>>
>>> but what I would rather do is this
>>>
>>> List<PropertyObject<T>> props = new List<PropertyObject>();
>>> props.Add(new PropertyObject<string>("goot"));
>>> props.Add(new PropertyObject<int>(20));
>>>
>>> where the List<> props can only contain PropertyObject<T>'s of any T
>>>
>>> Is this posible or does List<> have to contain only PropertyObjects of
>>> the same generic type parameter and in which case I have to use an
>>> ArrayList?
>>>

>>
>>

>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      2nd Dec 2005
As Kevin states, you cannot do it directly, however you may be able do it
indirectly.

Have PropertyObject<T> inherit from a base class. Then use this base class
in List<T>.

Something like:

abstract class PropertyObject
{
// common behavior for a property object
}

class PropertyObject<T> : PropertyObject
{
// T specific behavior for a property object
}

List<PropertyObject> props = new List<PropertyObject>();
props.Add(new PropertyObject<string>("goot"));
props.Add(new PropertyObject<int>(20));

PropertyObject would contain all the common behavior that you expect to use
polymorphically via the List<T>, while PropertyObject<T> would contain all
the T specific behavior for a property.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"msnews.microsoft.com" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
|I got (what I hope to be is) a simple question....
|
| I have a class called PropertyObject<T>
|
| so in my code I created something like this..
|
| name = new PropertyObject<string>("goot");
| age = new PropertyObject<int>(20);
|
| but what I would rather do is this
|
| List<PropertyObject<T>> props = new List<PropertyObject>();
| props.Add(new PropertyObject<string>("goot"));
| props.Add(new PropertyObject<int>(20));
|
| where the List<> props can only contain PropertyObject<T>'s of any T
|
| Is this posible or does List<> have to contain only PropertyObjects of the
| same generic type parameter and in which case I have to use an ArrayList?
|
|


 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      2nd Dec 2005
Nice idea, Jay. It is important to note, however, that Reflection would
still need to be used to discover the type of the particular version of
"PropertyObject" that would be in the list. Or, assuming that the base class
contained all the characteristics necessary to work with the objects while
in the List, Reflection could be termporarily unnecessary, at least until
the typed members of the objects in the List were used. In other words,
nothing would really be gained by using Generics here. A simple base class
and a number of derived classes would work just as well, since the Generic
List object would not contain any type information about what is stored in
it, other than the base type.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> As Kevin states, you cannot do it directly, however you may be able do it
> indirectly.
>
> Have PropertyObject<T> inherit from a base class. Then use this base class
> in List<T>.
>
> Something like:
>
> abstract class PropertyObject
> {
> // common behavior for a property object
> }
>
> class PropertyObject<T> : PropertyObject
> {
> // T specific behavior for a property object
> }
>
> List<PropertyObject> props = new List<PropertyObject>();
> props.Add(new PropertyObject<string>("goot"));
> props.Add(new PropertyObject<int>(20));
>
> PropertyObject would contain all the common behavior that you expect to
> use
> polymorphically via the List<T>, while PropertyObject<T> would contain all
> the T specific behavior for a property.
>
> --
> Hope this helps
> Jay [MVP - Outlook]
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "msnews.microsoft.com" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> |I got (what I hope to be is) a simple question....
> |
> | I have a class called PropertyObject<T>
> |
> | so in my code I created something like this..
> |
> | name = new PropertyObject<string>("goot");
> | age = new PropertyObject<int>(20);
> |
> | but what I would rather do is this
> |
> | List<PropertyObject<T>> props = new List<PropertyObject>();
> | props.Add(new PropertyObject<string>("goot"));
> | props.Add(new PropertyObject<int>(20));
> |
> | where the List<> props can only contain PropertyObject<T>'s of any T
> |
> | Is this posible or does List<> have to contain only PropertyObjects of
> the
> | same generic type parameter and in which case I have to use an
> ArrayList?
> |
> |
>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      2nd Dec 2005
Kevin,
| Nice idea, Jay. It is important to note, however, that Reflection would
| still need to be used to discover the type of the particular version of
| "PropertyObject" that would be in the list.
If by Reflection you mean Object.GetType or "is" or "as", then I agree, you
need use Reflection to know the specific type of object in the list. As you
state normally with polymorphic code you don't need to know the specific
type of an object in the list. I hope you would agree that would defeat the
purpose of having polymorphic code.

| In other words,
| nothing would really be gained by using Generics here.
The "Gain" of Generics here is you don't have to explicitly declare
StringPropertyObject, IntPropertyObject, DoublePropertyObject, etc... They
are implicitly declared via the generic type PropertyObject<T>. In fact I
would hope for any T, you now have a specific PropertyObject (bearing in
mind any constraints that T might have). Of course if you need a truely
specialized version of PropertyObject you could define that in addition to
PropertyObject<T>.

One could define normal virtual methods on PropertyObject so I could use the
items in List<PropertyObject> polymophically, just as I would in .NET 1.0 or
1.1. This may however mean that PropertyObject has an object version of a
method/property, where PropertyObject<T> has a T version of the
method/property. Something like:

abstract class PropertyObject
{
// common behavior for a property object

public abstract object Value { get; set; }
}

class PropertyObject<T> : PropertyObject
{
// T specific behavior for a property object

private T m_value;

public PropertyObject(T value)
{
m_value = value;
}

public T TypedValue
{
get { return m_value; }
set { m_value = value; }
}

public override object Value
{
get { return TypedValue; }
set { TypedValue = (T)value; }
}
}

Of course the above could also be done with interfaces rather then a base
class, with an interface you can at least use explicit interface
implementation to avoid have both TypeValue & Value on PropertyObject<T>.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Nice idea, Jay. It is important to note, however, that Reflection would
| still need to be used to discover the type of the particular version of
| "PropertyObject" that would be in the list. Or, assuming that the base
class
| contained all the characteristics necessary to work with the objects while
| in the List, Reflection could be termporarily unnecessary, at least until
| the typed members of the objects in the List were used. In other words,
| nothing would really be gained by using Generics here. A simple base class
| and a number of derived classes would work just as well, since the Generic
| List object would not contain any type information about what is stored in
| it, other than the base type.
|
| --
| HTH,
|
| Kevin Spencer
| Microsoft MVP
| .Net Developer
| You can lead a fish to a bicycle,
| but you can't make it stink.
|
| "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in
| message news:(E-Mail Removed)...
| > As Kevin states, you cannot do it directly, however you may be able do
it
| > indirectly.
| >
| > Have PropertyObject<T> inherit from a base class. Then use this base
class
| > in List<T>.
| >
| > Something like:
| >
| > abstract class PropertyObject
| > {
| > // common behavior for a property object
| > }
| >
| > class PropertyObject<T> : PropertyObject
| > {
| > // T specific behavior for a property object
| > }
| >
| > List<PropertyObject> props = new List<PropertyObject>();
| > props.Add(new PropertyObject<string>("goot"));
| > props.Add(new PropertyObject<int>(20));
| >
| > PropertyObject would contain all the common behavior that you expect to
| > use
| > polymorphically via the List<T>, while PropertyObject<T> would contain
all
| > the T specific behavior for a property.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "msnews.microsoft.com" <(E-Mail Removed)> wrote in message
| > news:(E-Mail Removed)...
| > |I got (what I hope to be is) a simple question....
| > |
| > | I have a class called PropertyObject<T>
| > |
| > | so in my code I created something like this..
| > |
| > | name = new PropertyObject<string>("goot");
| > | age = new PropertyObject<int>(20);
| > |
| > | but what I would rather do is this
| > |
| > | List<PropertyObject<T>> props = new List<PropertyObject>();
| > | props.Add(new PropertyObject<string>("goot"));
| > | props.Add(new PropertyObject<int>(20));
| > |
| > | where the List<> props can only contain PropertyObject<T>'s of any T
| > |
| > | Is this posible or does List<> have to contain only PropertyObjects of
| > the
| > | same generic type parameter and in which case I have to use an
| > ArrayList?
| > |
| > |
| >
| >
|
|


 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      2nd Dec 2005
Hi Jay,

I don't see it. Either you use the typed version of the value, in which case
you have to know the type, or you use the object version of the value in
which case you still don't know the type. In the first case, how does the
client know the type without using Reflection? In the second case, while the
client may be able to get the value, the type is still not known, and
Reflection is necessary to determine it. So, I still don't see any use for
Generics in this case. I could be wrong, though. Been quite busy today.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> Kevin,
> | Nice idea, Jay. It is important to note, however, that Reflection would
> | still need to be used to discover the type of the particular version of
> | "PropertyObject" that would be in the list.
> If by Reflection you mean Object.GetType or "is" or "as", then I agree,
> you
> need use Reflection to know the specific type of object in the list. As
> you
> state normally with polymorphic code you don't need to know the specific
> type of an object in the list. I hope you would agree that would defeat
> the
> purpose of having polymorphic code.
>
> | In other words,
> | nothing would really be gained by using Generics here.
> The "Gain" of Generics here is you don't have to explicitly declare
> StringPropertyObject, IntPropertyObject, DoublePropertyObject, etc... They
> are implicitly declared via the generic type PropertyObject<T>. In fact I
> would hope for any T, you now have a specific PropertyObject (bearing in
> mind any constraints that T might have). Of course if you need a truely
> specialized version of PropertyObject you could define that in addition to
> PropertyObject<T>.
>
> One could define normal virtual methods on PropertyObject so I could use
> the
> items in List<PropertyObject> polymophically, just as I would in .NET 1.0
> or
> 1.1. This may however mean that PropertyObject has an object version of a
> method/property, where PropertyObject<T> has a T version of the
> method/property. Something like:
>
> abstract class PropertyObject
> {
> // common behavior for a property object
>
> public abstract object Value { get; set; }
> }
>
> class PropertyObject<T> : PropertyObject
> {
> // T specific behavior for a property object
>
> private T m_value;
>
> public PropertyObject(T value)
> {
> m_value = value;
> }
>
> public T TypedValue
> {
> get { return m_value; }
> set { m_value = value; }
> }
>
> public override object Value
> {
> get { return TypedValue; }
> set { TypedValue = (T)value; }
> }
> }
>
> Of course the above could also be done with interfaces rather then a base
> class, with an interface you can at least use explicit interface
> implementation to avoid have both TypeValue & Value on PropertyObject<T>.
>
> --
> Hope this helps
> Jay [MVP - Outlook]
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Kevin Spencer" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> | Nice idea, Jay. It is important to note, however, that Reflection would
> | still need to be used to discover the type of the particular version of
> | "PropertyObject" that would be in the list. Or, assuming that the base
> class
> | contained all the characteristics necessary to work with the objects
> while
> | in the List, Reflection could be termporarily unnecessary, at least
> until
> | the typed members of the objects in the List were used. In other words,
> | nothing would really be gained by using Generics here. A simple base
> class
> | and a number of derived classes would work just as well, since the
> Generic
> | List object would not contain any type information about what is stored
> in
> | it, other than the base type.
> |
> | --
> | HTH,
> |
> | Kevin Spencer
> | Microsoft MVP
> | .Net Developer
> | You can lead a fish to a bicycle,
> | but you can't make it stink.
> |
> | "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in
> | message news:(E-Mail Removed)...
> | > As Kevin states, you cannot do it directly, however you may be able do
> it
> | > indirectly.
> | >
> | > Have PropertyObject<T> inherit from a base class. Then use this base
> class
> | > in List<T>.
> | >
> | > Something like:
> | >
> | > abstract class PropertyObject
> | > {
> | > // common behavior for a property object
> | > }
> | >
> | > class PropertyObject<T> : PropertyObject
> | > {
> | > // T specific behavior for a property object
> | > }
> | >
> | > List<PropertyObject> props = new List<PropertyObject>();
> | > props.Add(new PropertyObject<string>("goot"));
> | > props.Add(new PropertyObject<int>(20));
> | >
> | > PropertyObject would contain all the common behavior that you expect
> to
> | > use
> | > polymorphically via the List<T>, while PropertyObject<T> would contain
> all
> | > the T specific behavior for a property.
> | >
> | > --
> | > Hope this helps
> | > Jay [MVP - Outlook]
> | > .NET Application Architect, Enthusiast, & Evangelist
> | > T.S. Bradley - http://www.tsbradley.net
> | >
> | >
> | > "msnews.microsoft.com" <(E-Mail Removed)> wrote in message
> | > news:(E-Mail Removed)...
> | > |I got (what I hope to be is) a simple question....
> | > |
> | > | I have a class called PropertyObject<T>
> | > |
> | > | so in my code I created something like this..
> | > |
> | > | name = new PropertyObject<string>("goot");
> | > | age = new PropertyObject<int>(20);
> | > |
> | > | but what I would rather do is this
> | > |
> | > | List<PropertyObject<T>> props = new List<PropertyObject>();
> | > | props.Add(new PropertyObject<string>("goot"));
> | > | props.Add(new PropertyObject<int>(20));
> | > |
> | > | where the List<> props can only contain PropertyObject<T>'s of any
> T
> | > |
> | > | Is this posible or does List<> have to contain only PropertyObjects
> of
> | > the
> | > | same generic type parameter and in which case I have to use an
> | > ArrayList?
> | > |
> | > |
> | >
> | >
> |
> |
>
>



 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      2nd Dec 2005
Thanks for all your suggestions, I ended up making a keyed collection of the
PropertyObjects
below is the what I doing... this is just a learning exercise. the User
class (at the bottom) is the resultant use of the mess above it .
I have no idea if this is good practice but it was be interesting to play
with.

class NameObjectCollectionBase: PropertyObjectCollection {
public T GetValue<T>(string name) {}
public void SetValue<T>(string name, T value){}
public PropertyObject<T> GetProperty<T>(string name);
public PropertyObject<T> Add<T>(string name, T value){}

public void Undo() {
foreach(object o in InnerList)
((IUndo)o).Undo());

return success;
}
public void Redo(){
foreach(object o in InnerList)
((IRedo)o).Redo());

return success;
}
}

class abstract ComponentBase : IUndo, IRedo {
protected virtual PropertyObjectCollection Properties {get;}

public ObjectStatus Status {get;} //changed, unchanged, new, deleted...
etc.

public virtual void Undo(){
Properties.Undo();
}
public virtual void Redo(){
Properties.Redo();
}

public abstract void Save();
}

class User : ComponentBase, ILoad {
public int ID {
get{return Properties.GetValue<int>("ID", -1);}
}
public string Name {
get{return Properties.GetValue<string>("Name");}
set{Properties.SetValue<String>("Name", value);}
}

public int Age {
get{return Properties.GetValue<int>("Age");}
set{Properties.SetValue<int>("Age", value);}
}

public override void Save() {
DataProvider.Instance().SaveUser(Properties);
}

void ILoad.Load() {
DataProvider.Instance().LoadUser(Properties);
}
}

"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Jay,
>
> I don't see it. Either you use the typed version of the value, in which
> case you have to know the type, or you use the object version of the value
> in which case you still don't know the type. In the first case, how does
> the client know the type without using Reflection? In the second case,
> while the client may be able to get the value, the type is still not
> known, and Reflection is necessary to determine it. So, I still don't see
> any use for Generics in this case. I could be wrong, though. Been quite
> busy today.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> You can lead a fish to a bicycle,
> but you can't make it stink.
>
> "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
>> Kevin,
>> | Nice idea, Jay. It is important to note, however, that Reflection would
>> | still need to be used to discover the type of the particular version of
>> | "PropertyObject" that would be in the list.
>> If by Reflection you mean Object.GetType or "is" or "as", then I agree,
>> you
>> need use Reflection to know the specific type of object in the list. As
>> you
>> state normally with polymorphic code you don't need to know the specific
>> type of an object in the list. I hope you would agree that would defeat
>> the
>> purpose of having polymorphic code.
>>
>> | In other words,
>> | nothing would really be gained by using Generics here.
>> The "Gain" of Generics here is you don't have to explicitly declare
>> StringPropertyObject, IntPropertyObject, DoublePropertyObject, etc...
>> They
>> are implicitly declared via the generic type PropertyObject<T>. In fact I
>> would hope for any T, you now have a specific PropertyObject (bearing in
>> mind any constraints that T might have). Of course if you need a truely
>> specialized version of PropertyObject you could define that in addition
>> to
>> PropertyObject<T>.
>>
>> One could define normal virtual methods on PropertyObject so I could use
>> the
>> items in List<PropertyObject> polymophically, just as I would in .NET 1.0
>> or
>> 1.1. This may however mean that PropertyObject has an object version of a
>> method/property, where PropertyObject<T> has a T version of the
>> method/property. Something like:
>>
>> abstract class PropertyObject
>> {
>> // common behavior for a property object
>>
>> public abstract object Value { get; set; }
>> }
>>
>> class PropertyObject<T> : PropertyObject
>> {
>> // T specific behavior for a property object
>>
>> private T m_value;
>>
>> public PropertyObject(T value)
>> {
>> m_value = value;
>> }
>>
>> public T TypedValue
>> {
>> get { return m_value; }
>> set { m_value = value; }
>> }
>>
>> public override object Value
>> {
>> get { return TypedValue; }
>> set { TypedValue = (T)value; }
>> }
>> }
>>
>> Of course the above could also be done with interfaces rather then a base
>> class, with an interface you can at least use explicit interface
>> implementation to avoid have both TypeValue & Value on PropertyObject<T>.
>>
>> --
>> Hope this helps
>> Jay [MVP - Outlook]
>> .NET Application Architect, Enthusiast, & Evangelist
>> T.S. Bradley - http://www.tsbradley.net
>>
>>
>> "Kevin Spencer" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> | Nice idea, Jay. It is important to note, however, that Reflection would
>> | still need to be used to discover the type of the particular version of
>> | "PropertyObject" that would be in the list. Or, assuming that the base
>> class
>> | contained all the characteristics necessary to work with the objects
>> while
>> | in the List, Reflection could be termporarily unnecessary, at least
>> until
>> | the typed members of the objects in the List were used. In other words,
>> | nothing would really be gained by using Generics here. A simple base
>> class
>> | and a number of derived classes would work just as well, since the
>> Generic
>> | List object would not contain any type information about what is stored
>> in
>> | it, other than the base type.
>> |
>> | --
>> | HTH,
>> |
>> | Kevin Spencer
>> | Microsoft MVP
>> | .Net Developer
>> | You can lead a fish to a bicycle,
>> | but you can't make it stink.
>> |
>> | "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in
>> | message news:(E-Mail Removed)...
>> | > As Kevin states, you cannot do it directly, however you may be able
>> do
>> it
>> | > indirectly.
>> | >
>> | > Have PropertyObject<T> inherit from a base class. Then use this base
>> class
>> | > in List<T>.
>> | >
>> | > Something like:
>> | >
>> | > abstract class PropertyObject
>> | > {
>> | > // common behavior for a property object
>> | > }
>> | >
>> | > class PropertyObject<T> : PropertyObject
>> | > {
>> | > // T specific behavior for a property object
>> | > }
>> | >
>> | > List<PropertyObject> props = new List<PropertyObject>();
>> | > props.Add(new PropertyObject<string>("goot"));
>> | > props.Add(new PropertyObject<int>(20));
>> | >
>> | > PropertyObject would contain all the common behavior that you expect
>> to
>> | > use
>> | > polymorphically via the List<T>, while PropertyObject<T> would
>> contain
>> all
>> | > the T specific behavior for a property.
>> | >
>> | > --
>> | > Hope this helps
>> | > Jay [MVP - Outlook]
>> | > .NET Application Architect, Enthusiast, & Evangelist
>> | > T.S. Bradley - http://www.tsbradley.net
>> | >
>> | >
>> | > "msnews.microsoft.com" <(E-Mail Removed)> wrote in message
>> | > news:(E-Mail Removed)...
>> | > |I got (what I hope to be is) a simple question....
>> | > |
>> | > | I have a class called PropertyObject<T>
>> | > |
>> | > | so in my code I created something like this..
>> | > |
>> | > | name = new PropertyObject<string>("goot");
>> | > | age = new PropertyObject<int>(20);
>> | > |
>> | > | but what I would rather do is this
>> | > |
>> | > | List<PropertyObject<T>> props = new List<PropertyObject>();
>> | > | props.Add(new PropertyObject<string>("goot"));
>> | > | props.Add(new PropertyObject<int>(20));
>> | > |
>> | > | where the List<> props can only contain PropertyObject<T>'s of any
>> T
>> | > |
>> | > | Is this posible or does List<> have to contain only PropertyObjects
>> of
>> | > the
>> | > | same generic type parameter and in which case I have to use an
>> | > ArrayList?
>> | > |
>> | > |
>> | >
>> | >
>> |
>> |
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generic collection of collection sort help rodchar Microsoft C# .NET 4 2nd Jun 2009 11:15 PM
The generic type 'System.Collections.ObjectModel.Collection`1' was used with the wrong number of generic arguments in assembly 'mscorlib' svkuery@gmail.com Microsoft Dot NET Framework 1 28th Mar 2007 08:05 PM
Collection of generic objects that can be xml serialized? Craig Buchanan Microsoft VB .NET 1 25th Sep 2006 02:12 PM
Generic Collection of deferred generic types Steven Cummings Microsoft C# .NET 8 17th Feb 2006 10:42 PM
how to setup a generic base class to fill collection objects? rapataa Microsoft C# .NET 1 3rd Jun 2005 08:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:50 PM.