473,695 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Inheritance : Creating derived classes from instances of base class.

Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collecti ons.Arraylist allAnimals =
ThirdPartyORMTo ol.GetAllAnimal s();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collecti ons.Arraylist allTaggedAnimal s =
TagAllAnimals(a llAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSo urce = allTaggedAnimal s;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTo ol.SaveAnimals( allTaggedAnimal s);
ThirdPartyORMTo ol.DoSomeOtherA nimalRelatedStu ff(allTaggedAni mals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(An imal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

Jan 19 '07 #1
26 5361
<ny********@hot mail.comwrote in message
news:11******** *************@a 75g2000cwd.goog legroups.com...
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}
I would create a copy constructor in the base Animal class that takes in an
Animal and sets all the data in 'this' with the values from the passed in
instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(An imal an) : base (an)
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collecti ons.Arraylist allAnimals =
ThirdPartyORMTo ol.GetAllAnimal s();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collecti ons.Arraylist allTaggedAnimal s =
TagAllAnimals(a llAnimals);
In here, for each instance of Animal in allAnimals you would create a
TaggedAnimal, passing in the Animal to the constructor.
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSo urce = allTaggedAnimal s;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTo ol.SaveAnimals( allTaggedAnimal s);
ThirdPartyORMTo ol.DoSomeOtherA nimalRelatedStu ff(allTaggedAni mals);

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(An imal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}
You can't assign to the base instance like this. You could assign
properties in base based on the same property value in currentAnimal, or add
the copy constructor to Animal as I have stated above.
Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.
You can't. Once you are within your derived classes constructor, your base
instance is already created. You can't assign a new base instance.
--
Tom Porterfield

Jan 19 '07 #2
Tom Porterfield wrote:
<ny********@hot mail.comwrote in message
news:11******** *************@a 75g2000cwd.goog legroups.com...
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

I would create a copy constructor in the base Animal class that takes in an
Animal and sets all the data in 'this' with the values from the passed in
instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}

Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(An imal an) : base (an)
Unfortunately I can't create a copy constructor. Thats why i said in
the original post "because I am not always able to control/create all
the different constructors the base class has.". The problem is that
the base classes are auto generated for me in the data access layer by
the ORM tool. I don't want to modify auto generated code. And
modifying/tweaking the ORM to add custom code doesn't seem very elegant
either.
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collecti ons.Arraylist allAnimals =
ThirdPartyORMTo ol.GetAllAnimal s();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collecti ons.Arraylist allTaggedAnimal s =
TagAllAnimals(a llAnimals);

In here, for each instance of Animal in allAnimals you would create a
TaggedAnimal, passing in the Animal to the constructor.
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSo urce = allTaggedAnimal s;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTo ol.SaveAnimals( allTaggedAnimal s);
ThirdPartyORMTo ol.DoSomeOtherA nimalRelatedStu ff(allTaggedAni mals);

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(An imal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

You can't assign to the base instance like this. You could assign
properties in base based on the same property value in currentAnimal, or add
the copy constructor to Animal as I have stated above.
Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

You can't. Once you are within your derived classes constructor, your base
instance is already created. You can't assign a new base instance.
That does seem to be the case, but can we intercept it before the
derived constructor is called and pass it a new base somehow, rather
than letting the CLR instantiate a new base. Something along the lines
of

public TaggedAnimal(An imal currentAnimal) : base = currentAnimal
{

}

I would prefer not to use reflection as it will be expensive to run
over a collection of objects

--
Tom Porterfield
Jan 19 '07 #3

ny********@hotm ail.com wrote:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collecti ons.Arraylist allAnimals =
ThirdPartyORMTo ol.GetAllAnimal s();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collecti ons.Arraylist allTaggedAnimal s =
TagAllAnimals(a llAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSo urce = allTaggedAnimal s;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTo ol.SaveAnimals( allTaggedAnimal s);
ThirdPartyORMTo ol.DoSomeOtherA nimalRelatedStu ff(allTaggedAni mals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(An imal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.
So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?

Jan 19 '07 #4
I had a quick look at the GOF book, to no avail....

Bruce Wood wrote:
ny********@hotm ail.com wrote:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collecti ons.Arraylist allAnimals =
ThirdPartyORMTo ol.GetAllAnimal s();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collecti ons.Arraylist allTaggedAnimal s =
TagAllAnimals(a llAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSo urce = allTaggedAnimal s;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTo ol.SaveAnimals( allTaggedAnimal s);
ThirdPartyORMTo ol.DoSomeOtherA nimalRelatedStu ff(allTaggedAni mals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(An imal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?
Jan 19 '07 #5
"Bruce Wood" <br*******@cana da.comschrieb im Newsbeitrag
news:11******** **************@ v45g2000cwv.goo glegroups.com.. .
>
ny********@hotm ail.com wrote:
>Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collect ions.Arraylist allAnimals =
ThirdPartyORMT ool.GetAllAnima ls();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimal s */
System.Collect ions.Arraylist allTaggedAnimal s =
TagAllAnimals( allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataS ource = allTaggedAnimal s;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMT ool.SaveAnimals (allTaggedAnima ls);
ThirdPartyORMT ool.DoSomeOther AnimalRelatedSt uff(allTaggedAn imals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(An imal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?
The only way, this can be done is by either:

- add the method to the code of the base class (wich surely is only
possible, if one has access to that code.)

- use a static method, that has a first parameter of type Animal. That does
the same, with some restrictions: It can't be used in delegate creation as a
insctance method, with an instance expression of Animal. But in C# 2.0 this
can be solved by an anonymous method wich catches a variable of type Animal.

In C# 3.0 this method could be declared as extension method, so it can be
called as if it were an instance method.

Christof
Jan 19 '07 #6
Would you be willing to wrap your Animal objects in TaggedAnimal clothing?
In other words your TaggedAnimal would contain an Animal. This permits you
to reference the tag properties/methods through the TA reference but still
reference Animal properties via the Animal object it contains.

myAnimal.Tag()
myAnimal.Animal .AnimalProperty

The collection would work the same way, methods you wrote are in the
TaggedAnimals collection which contains a collection of Animals. You could
even hide your ThirdPartyORMTo ol methods in the TaggedAnimal Collection
class.

TaggedAnimalCol lection.GetAllA nimals() would fill the inner collection.
<ny********@hot mail.comwrote in message
news:11******** *************@s 34g2000cwa.goog legroups.com...
>I had a quick look at the GOF book, to no avail....

Bruce Wood wrote:
>ny********@hotm ail.com wrote:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collecti ons.Arraylist allAnimals =
ThirdPartyORMTo ol.GetAllAnimal s();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collecti ons.Arraylist allTaggedAnimal s =
TagAllAnimals(a llAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSo urce = allTaggedAnimal s;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTo ol.SaveAnimals( allTaggedAnimal s);
ThirdPartyORMTo ol.DoSomeOtherA nimalRelatedStu ff(allTaggedAni mals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(An imal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

So... if I understand you correctly, you want to add a certain piece of
functionalit y across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?

Jan 19 '07 #7
This looks to me like Decorator, but Decorator requires that you
inherit from each class, which is what you're trying to avoid, if I
understand you correctly.

A question for the folks who are on .NET 2.0: Can you do this with
generics:

public class GenericDecorato r<Twhere T : Animal
{
...
}

? That is, can a generic class inherit from its own type parameter?

ny********@hotm ail.com wrote:
I had a quick look at the GOF book, to no avail....

Bruce Wood wrote:
ny********@hotm ail.com wrote:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...
>
/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}
>
}
>
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal
>
{
Public string GetTag()
{/**/}
>
}
>
>
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.
>
So the main code would look something like this :
>
public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collecti ons.Arraylist allAnimals =
ThirdPartyORMTo ol.GetAllAnimal s();
>
/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collecti ons.Arraylist allTaggedAnimal s =
TagAllAnimals(a llAnimals);
>
>
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSo urce = allTaggedAnimal s;
>
/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTo ol.SaveAnimals( allTaggedAnimal s);
ThirdPartyORMTo ol.DoSomeOtherA nimalRelatedStu ff(allTaggedAni mals);
>
>
}
>
}
>
How would I go about doing this? Basically, my first instinct was to do
something like this
>
public class TaggedAnimal : Animal
>
{
>
/* Pass an instance of the base class to the constructor */
public TaggedAnimal(An imal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}
>
Public string GetTag()
{/**/}
>
}
>
>
but in C# .net 2.0, it complains that "base is a not valid in this
context".
>
I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.
>
Any Help is much appreciated.
So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?
Jan 19 '07 #8
I don't believe so, it is simple containment. He can create a TaggedAnimal
class that has one property an Animal object. He can add methods that add
"tag" behavior and can reference the Animal property if he ever needs to
pass the Animal itself to something outside of his control.

As I understood it his own routines understand TaggedAnimal objects but some
of the existing routines only accept Animal objects. These routines aren't
going to understand anything about tagging in any case so it doesn't need
(nor want) a TaggedAnimal.

Who knows? :-)
"Bruce Wood" <br*******@cana da.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
This looks to me like Decorator, but Decorator requires that you
inherit from each class, which is what you're trying to avoid, if I
understand you correctly.

A question for the folks who are on .NET 2.0: Can you do this with
generics:

public class GenericDecorato r<Twhere T : Animal
{
...
}

? That is, can a generic class inherit from its own type parameter?

Jan 19 '07 #9

Tom Leylan wrote:
I don't believe so, it is simple containment. He can create a TaggedAnimal
class that has one property an Animal object. He can add methods that add
"tag" behavior and can reference the Animal property if he ever needs to
pass the Animal itself to something outside of his control.

As I understood it his own routines understand TaggedAnimal objects but some
of the existing routines only accept Animal objects. These routines aren't
going to understand anything about tagging in any case so it doesn't need
(nor want) a TaggedAnimal.

Who knows? :-)
Maybe I misunderstood his original problem, then.

As I understand it, he has an ORM that is generating lots of different,
unrelated classes from data representations . He wants to graft on a Tag
property (or some other method / property / whatever) to every class
and then use the resulting class interchangeably with the original
class. The issue, as I understand it, is that he doesn't want to have
to write all of the subclasses individually, by hand, and maintain
them, and he doesn't want to have to modify the base classes, which are
automatically generated.

I could be wrong, though.

Another solution might be partial classes in C# 2.0: just modify every
automatically generated class to be partial, then write any additional
code in another file, for each class. At least then the additions
wouldn't be overwritten when you used the ORM tool to regenerate the
classes. In fact, this is how the ORM tool should be doing things:
generating partial classes that you can then expand at will.

Jan 19 '07 #10

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

Similar topics

10
1880
by: cppaddict | last post by:
Hi, Say I have a abstract base class and I know that every derived class will have a static data member (call it SharedInformation) representing information to be shared across all instances of that derived class. However, this information will be differenet for each derived class. Now, if I define SharedInformation as a static member of the base class, then SharedInformation cannot vary across the different derived
7
1624
by: Mahesh | last post by:
Hello, I am having a query about inheritance. I have a virtual base class that is derived by to other classes that are hirarchially at the same level. But I still need to ensure that only a single instance of the base class is created. Is this even possible? If yes then how do I go about doing this? Thanks, Mahesh
12
1718
by: Taylor | last post by:
I'm trying to understand inheritance. I'd like to make my own type of IPAddress lets call it myIp. The following gives me CS0029 error: Cannot implicitly convert type 'System.Net.IPAddress' to 'Inheritance_Testing.myIp' Could you steer me in the right direction? class myIp : System.Net.IPAddress
45
6342
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
8
2885
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
12
2370
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two consecutive layers of diamond-shaped inheritance; the first layer is declared as using virtual inheritance, while the second one is declared as *not* using it. This is what I'd like to have:
3
2551
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
13
2489
by: stephenpas | last post by:
We are trying to monkey-patch a third-party library that mixes new and old-style classes with multiple inheritance. In so doing we have uncovered some unexpected behaviour: <quote> class Foo: pass class Bar(object): pass
4
3955
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
8619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8559
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8826
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8818
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7652
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6487
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5832
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.