Inheritence issue, this has to be possible...
I feel like I should know this really easily but I cant figure out how to do it.
Basically I have an entity, lets say:
public class MyEntity
{
public string somevar = "whatever";
}
and one that builds onto the functionality of it.
public class MyExtendedEntity : MyEntity
{
public string somevar2 = "whatever2";
}
Now, lets say I have this in form load
FORMLOAD
{
MyEntity myEnt = new MyEntity();
//Problem HERE
// How do I make an instance of MyExtendedEntity which contains myEnt as its base?
}
So to summarize, I need to create an instance of a class that inherits another class. easy enough. But I dont want to create a freshly initialized class, I want to beable to make the base of myExtendedEntity to an instance of myEntity without making a constructor that does something like this:
// I dont want to have to do this!
MyExtendedEntity(MyEntity myEnt)
{
base.somevar = myEnt.somevar;
}
essentially (this wont work) I need something like this
MyExtendedEntity(MyEntity myEnt)
{
base = myEnt;
}
Someone please point me in the right way, this seems utterly simple. I must be missing something.
[1290 byte] By [
migee] at [2007-11-20 11:11:16]

# 1 Re: Inheritence issue, this has to be possible...
I may not understand what it is you need... but:
<code>
MyExtendedEntity(MyEntity myEnt)
</code>
Why are you passing MyEntity as a parameter to MyExtenededEntity?
# 2 Re: Inheritence issue, this has to be possible...
I am not exactly sure what you are asking... but fundementally what you seem to be asking doesn't make sense.
MyEnt is an INSTANCE.
MyEntity is a TYPE.
Consider a biological chart:
You have Animals, which have Mammals, which have Cats.
Then you make an instance of Cat, called Fluffy.
Now you want to go back to your chart and figure out where Fluffy goes. You might be able to see fluffy under Cat, but it doesn't quite make sense because the chart is an abstraction of "the kind of" things.
It is true that Fluffy is a kind of Cat, but unto itself it doesn't make sense to think of another Cat as a Kind of Fluffy. So now you want to find some way to shove Fluffy into that chart beween Mammal and Cat. You would then need to start thinking of Kind of fluffies, which make no sense.
Hopefully this brings clarity to some degree, or perhaps it totally confused you.
--------------
What I think you *MIGHT* want is Generics.
Lets say you have two classes, Alpha and Beta that have some property which is thier name. IE: Alpha.MyName.
Now you want some other class to use one of these as a TYPE.
as a Generic you would have:
MyClass<T>
{ private T MyObject;
MyClass() { MyObject = new T(); }
void printName() { Console.Writeline(MyObject.MyName);}
}
Then in Main:
void Main()
{
MyClass<Alpha> Obj = new MyClass<Alpha>();
Obj.printName();
}
DeepT at 2007-11-9 11:37:29 >

# 3 Re: Inheritence issue, this has to be possible...
What you want is impossible unfortunately.
MyExtendedEntity(MyEntity myEnt)
{
base.somevar = myEnt.somevar;
}
That is the closest you can get to what you want, and would also make the most sense logically.
The best solution would probably be:
class MyEntity()
{
string name;
public MyEntity()
{
}
// Sub classes will call this constructor so the data can be copied from
// 'val' to this new object.
protected MyEntity(MyEntity val)
{
this.name = val.name;
}
}
class MyExtendedEntity : MyEntity
{
public MyExtendedEntity(MyEntity other)
: base (other) // Call the base classes constructor with the MyEntity object
{
}
}
Basically when you want to create a new ExtendedEntity out of an Entity, just call: new MyExtendedEntity(originalEntity) and the object will be constructed for you. It would be the cleanest way to get what you want (i think).
# 4 Re: Inheritence issue, this has to be possible...
Thanks for trying to deal with me poorly explaining my problem :)
The problem was that my coworker had instances of a class he was sending over. I wanted to build a class to inherit his class so I could add my own functionality. But the thing was, he was already sending instances of the (base) class in this case and I needed to somehow fill my instances with the base data.
DeepT: I understand that one is a type and the other an instance. My wording wasnt the best, sorry. I'm not great with generics but when im not busy I'll take a look into that example to see what your trying to get accross
Mutant_Fruit: I think you read my mind. I wanted to get around initializing all the base values in the constructor. It seemed logical to do otherwise because I already had all the values in a class which matched the base of my extended class. It still feels like there must be a way to do this but I guess you guys are right.
Thanks all for helping.
migee at 2007-11-9 11:39:24 >

# 5 Re: Inheritence issue, this has to be possible...
Hmm, there is a way you can do what you want if the other programmer is cooperative.
First, he has to let you inherit from the class. IE: He wrote Cat, and you want to make SiameseCat.
Class SiameseCat : Cat
The next thing he needs to provide is a copy constructor, that is a constructor for Cat that takes "aCat" as an operand and creates a duplicate of the object passed in.
Now I am not sure what the exact C# syntax for this is, I only know how to do it in C++.
In C++ you would do something like:
Class SiameaseCat::Cat
SiameaseCat(Cat &aCat):Cat(aCat)
{ ... }
}
Its been a while, but I think that is the correct syntax. Basically it calls the base class constructor which initializes the base class from a copy of aCat, and then you can do what it is you want from that point on.
DeepT at 2007-11-9 11:40:23 >

# 6 Re: Inheritence issue, this has to be possible...
// How do I make an instance of MyExtendedEntity which contains myEnt as its base?
I think youre not understanding inheritance. MyExtendedEntiry IS A MyEntity. It doesnt HAS A MyEntity
You IS A Person
You dont HAS A Person (unless youre a girl, and youre pregnant, in which case one of your member variables is another person. This is the only time when migee HAS A Person.. all the time, migee IS A Person
So to summarize, I need to create an instance of a class that inherits another class. easy enough. But I dont want to create a freshly initialized class, I want to beable to make the base of myExtendedEntity to an instance of myEntity without making a constructor that does something like this:
You mean you want to convert an existing Person, into a Migee. Youre going to have to provide a helper method:
Person p = new Person();
Migee m = new Migee();
m.CopyFrom(p);
migee now has copied all the attributes of person. We know that a Migee can store all the details that a Person can store, because Migee IS A Person
// I dont want to have to do this!
MyExtendedEntity(MyEntity myEnt)
{
base.somevar = myEnt.somevar;
}
essentially (this wont work) I need something like this
MyExtendedEntity(MyEntity myEnt)
{
base = myEnt;
}
I think youre really not understanding inheritance. You cant just make your next door neighbour into your dad by clicking your fingers. Basically what i'm trying to say here is that:
There is a Person, lets say his name is fred
And there is also a Migee (who is at the same time, another Person and a specialised person at that)
When you make a new Migee, .NET does NOT create 2 objects (1 a Person, 1 a Migee) and link the two. It creates one object, that has all the features of a person, and additionally the features of a migee. You cant just "replace" the Person part of an instance of Migee, with another Person object (to make fred your dad)
Instead of telling us this problem, try telling us exactly what problem youre trying to solve, none of this generic MyEnt, SomeVar stuff.. Tell us the actual problem you have, and we'll tell you whether solving it with inheritance is the best idea
cjard at 2007-11-9 11:41:33 >

# 7 Re: Inheritence issue, this has to be possible...
Thanks for trying to deal with me poorly explaining my problem :)
The problem was that my coworker had instances of a class he was sending over. I wanted to build a class to inherit his class so I could add my own functionality. But the thing was, he was already sending instances of the (base) class in this case and I needed to somehow fill my instances with the base data.
I had this same problem once, and I made a CopyFrom method (or constructor, dont matter)
Just remember that you cannot access private members in the passed in entity; thats the whole point of OO language security, that you cannot fiddle with another object's data directly, but instead must use only what that object chooses to expose.
The easiest solution actually, is to give your specialised class to that original dveloper and when he is sending you things, he can send you instances of your class, not his (He can send you Migee instances, not Person)
cjard at 2007-11-9 11:42:27 >

