• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 840
  • Last Modified:

Inheritance : Assign a Parent Object to a Inherited Child Object.

Hi all,
I would like to assign a Parent Object to a Inherited Child Object. Is this possible.

Given a Parent and a Child Class A and B : A respectively

The assignments permitted for such as relationship by default are

A = (A) B

but B = (B) A is illegal

Is there a way around this that doesnt involve heavy implicit or expliciat operator functions.

Regards

Paul
0
adavir
Asked:
adavir
  • 6
  • 5
  • 2
  • +1
1 Solution
 
jonorossiCommented:
Not that I am aware of, this doesn't make a lot of sense. Maybe your inheritance hierarchy should be changed. I don't know if you know what you are saying, but it doesn't make sense to say every animal is a dog, however every dog is an animal. If you describe your classes I might be able to suggest something how you could change it or something else to convert a dog to an animal.
0
 
dkloeckCommented:
You can't do B = (B) A since B is more specific than A, however you could have a third class C with
B : C

then you can do:

B= (C)A
A= (C)B

 C could be object for example (all clases are childs of object in c#)
0
 
dkloeckCommented:
now that I think of it..that would not work neither, since it would give an error when executing because it is not in the right type.. No, there is no way to do it
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
adavirAuthor Commented:
Basically in my software

Every Member(user) has and Organisation that they are a member of

an organisation may be a Supplier, Shipper, Bond, Airport, Airline.

Depending in the type of organisation more specific attributes are required.

The types Supplier, Shipper, Bond, Airport, Airline inherit the type Organistaion.

The additional attributes that further defines the inheriting class are stored in the base Organisation Instance in the method OrganisationAttributes.

public class Supplier : Organisation
{
      /// <summary>
      /// Whether a supplier delivers their Own Products or not
      /// </summary>
      public bool SupplierDelivers
      {
            get
            {
                  return OrganisationAttributes[SupplierAttribute.SUPPLIER_DELIVERS];
            }
            set
            {
                  OrganisationAttributes[SupplierAttribute.SUPPLIER_DELIVERS] = value;
            }
      }
}

What complicates things a little is that all of my all of my organisations are located in a cached HashTable hashed by ID. So to fectah an Organisation very quickly I could go

Organisation tOrg = OrganisationStore.FromID(1827)   // and that would grab my organisation

however

Order.OrderSupplier = OrganisationStore.FromID(1827)  // Would not work

In a perfect world I would like to be able to Assign a Supplier to an Organisation and vice versa while maintaining the essential attributes in both cases. I thought my answer would lie in a few simple lines inside an Implicit opreator. but i guess not

Thanks for your input

But I dont think I can as in this ins



 
0
 
JimBrandleyCommented:
You can accomplish what you want to do. Instead of "assigning", I think you mean casting. Here's one way:
Order.OrderSupplier = OrganisationStore.FromID(1827)  as OrderSupplier;

If it is a different kind of organization, it will not throw a run-time exception. It will set:
Order.OrderSupplier = null;

So, if you know what kind of organization you are getting, that is enough. If not, then:
Supplier supplier = OrganisationStore.FromID(1827)  as Supplier;
Shipper shipper =  OrganisationStore.FromID(1827)  as Supplier;
etc.

At most one of them will be non-null. You could make it easer by adding something like OrganizationType to the Orgainization class, and set it from the derived class in the constructor. Then,
Organisation org = OrganisationStore.FromID(1827);
if (org.Organization == SupplierType)
   Order.OrderSupplier = org as Supplier;

Jim
0
 
JimBrandleyCommented:
Sorry last bit should be:
if (org.OrganizationType == SupplierType)
   Order.OrderSupplier = org as Supplier;

Jim
0
 
adavirAuthor Commented:
Thanks for the reply, i will have a look at it in the morning. After actually thinking about it I have a feelling that the answer lies with adding a base constructor in my inheriting classes.

I understand what you are suggesting but if you mean

Shipper shipper =  (Shipper) OrganisationStore.FromID(1827) ;

This will through an InValid cast exception, as you would expect. Maybe in VB it doesnt but CSharp definately does.

What I will try tomorrow is

public class Supplier : Organisation
{
      public Suppler() : base()
      {
           
      }

      public Supplier(Organisation pOrganisation) : base(Organisation pOrganisation)
      {
            // Nothing here
      }
}

But how can i populate my base class instance with the pOrganisation as...

public Organisation(Organisation pOrganisation)
{
      this = (Organisation) MemberwiseClone();
}


will definately not work, but may help you understand what I am trying to achieve. Whoever can answer this question solves the problem. (depending on how i get on in the morning with the base constructors.)

Regards

Paul


0
 
JimBrandleyCommented:
Don't cast like this:
Shipper shipper =  (Shipper) OrganisationStore.FromID(1827) ;

Instead, use this:
Shipper shipper =  OrganisationStore.FromID(1827) as Shipper ;
This retuns null if the cast is invalid, no exception thrown.

Make your constructors something like this:
public abstract class Organization
{
   public enum OrganizationType
   {
      None = 0,
      Supplier = 1,
      Shipper,
      Etc
   };

   private OrganizationType mType = OrganizationType.None;
   public OrganizationType OrgType
   {
      get
      {
         return mType;
      }
   }
   public Organization(OrganizationType orgType)
   {
      mType = orgType;
   }
}
public class Shipper : Organization
{
   public Shipper()
      : base(OrganizationType.Shipper)
   {
   }
}

Jim
0
 
adavirAuthor Commented:
Just tried your suggestion, it always returns NULL when assigning a Organisation to a Supplier.Thanks for pointing out what the "as" keyword is for I always thought it was a VB syntax of casting.

How can I overwrite the Current instance, I know the following wont work but is there a means of doing so?

public Organisation(Organisation pOrganisation)
{
      this = pOrganisation;
}

Regards Paul
0
 
adavirAuthor Commented:
Right, the answer for this is in the Base class Constructor. What I am trying to achieve is something like...

public Organisation(Organisation pOrganisation)
{
      this = pOrganisation;
}

How can this be achieved, if at all?

Paul
0
 
JimBrandleyCommented:
It looks like what you want is to create a copy of an Organization. The best way to do this is:
1. In the Organization class add this:
public abstract object Clone();

2. In each derived class, (in this example Shipper) add:
public override object Clone()
{
   Shipper newOne = new Shipper();
   newOne.Property1 = this.Property1;
   newOne.Property2 = this.Property2;
   // etc
   return newOne;
}

MS provides a shallow clone, but creating you own allows you to duplicate any child assignments and such.

Jim
0
 
adavirAuthor Commented:
Hi Jim,

Thats what I was previously doing, i just thought that there might be a better way of doing it. But it looks like there isnt.

The problem with solution you provided is that every time i add a new method i have to update that function. I have been doing that for years and always felt that there was maybe a way of doing it in a more generic way.

also I found out today that you cant create an implicit conversion from base to parent class even using the solution you provided.

Anyways seen as it is the actual solution to problem its best to end the thread

Regards

Paul
0
 
JimBrandleyCommented:
I have a lot of code in production that works just like this:
Organisation org = OrganisationStore.FromID(1827);
if (org.OrgType == SupplierType)
   Order.OrderSupplier = org as Supplier;

with no problems.

Jim
0
 
adavirAuthor Commented:
hmmm, everytime I ran it i got null everytime. I really tried to get that working spent 3 hours with your suggestion.

Thanks for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now