?
Solved

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

Posted on 2007-10-01
14
Medium Priority
?
821 Views
Last Modified: 2012-05-05
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
Comment
Question by:adavir
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 9

Expert Comment

by:jonorossi
ID: 19990659
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
 
LVL 10

Expert Comment

by:dkloeck
ID: 19990808
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
 
LVL 10

Expert Comment

by:dkloeck
ID: 19990829
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:adavir
ID: 19991065
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19993923
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19993937
Sorry last bit should be:
if (org.OrganizationType == SupplierType)
   Order.OrderSupplier = org as Supplier;

Jim
0
 

Author Comment

by:adavir
ID: 19994511
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19994648
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
 

Author Comment

by:adavir
ID: 19996995
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
 

Author Comment

by:adavir
ID: 19997040
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
 
LVL 22

Accepted Solution

by:
JimBrandley earned 1500 total points
ID: 20000208
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
 

Author Comment

by:adavir
ID: 20002054
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20002230
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
 

Author Comment

by:adavir
ID: 20005042
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question