locked
Initializing derived class with base one? RRS feed

  • Question

  • User8761146 posted

    Guys, I want to ask the simpliest question about architecture for today =) Seems too long though

    I have a class Altering:

    public class Altering
        {
            protected string characteristicName;

            protected short value;

    ...

    }

    and derived class OwnedAltering:


    public class OwnedAltering:Altering
        {
            private int ownerInd;

    ...

    }


    And I have a situation when I need to "cast" List<Altering> to List<OwnedAltering>.

    I decided to create method in base class Altering for this:


    public OwnedAltering AssignOwner(int ownerIndex)
            {
                OwnedAltering owned = new OwnedAltering();
                owned.characteristicName = this.characteristicName;
                owned.value = this.value;

                owned.OwnerInd = ownerIndex;

                return owned;
            }


    and then I do like this:

    foreach (Altering alter in AllPossibleAlterings)
                        {
                            OwnedAltering owned = alter.AssignOwner(i);
                            newList.Add(owned);
                        }  


    But I'm pretty sure there must be nicer and faster way.

    I will really appreciate ideas.

    Tuesday, February 23, 2010 5:00 PM

Answers

  • User-1149223197 posted

    Your AssignOwner method in the base Altering class is just creating a new object, so it really has nothing to do with inheritance.

    If you want to cast, use the "as OwnedAltering" test suggested above.

    If you need to create a new instance of OwnedAltering based on the base Altering instance in the foreach block, consider implementing the factory pattern. Create a class AlteringFactory that can create new (cloned) instances of OwnedAltering objects, using a base Altering instance as a parameter.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 6:05 PM
  • User-1149223197 posted

    Guys, but "as OwnedAltering" for Altering is nor gonna work! It always returns null.

    That is because the actual instances cannot be cast down the OwnedAltering types. It sounds like you need to create OwnedAltering objects using the base Altering types, just adding a new property. For this, you should create a static AlteringFactory class to construct these for you. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2010 5:33 AM

All replies

  • User1737159284 posted

    You could try this:



    foreach ( a in listOfAltering) {
    OwnedAltering oa = a as OwnedAltering;
    oa.OwnedInd = SomeInteger;
    listOfOwnedAltering.Add(oa);
    foreach ( a in listOfAltering) {
    			OwnedAltering oa = a as OwnedAltering;
    			oa.OwnedInd = SomeInteger;
    			listOfOwnedAltering.Add(oa);
    		}





    Tuesday, February 23, 2010 5:55 PM
  • User-1149223197 posted

    Your AssignOwner method in the base Altering class is just creating a new object, so it really has nothing to do with inheritance.

    If you want to cast, use the "as OwnedAltering" test suggested above.

    If you need to create a new instance of OwnedAltering based on the base Altering instance in the foreach block, consider implementing the factory pattern. Create a class AlteringFactory that can create new (cloned) instances of OwnedAltering objects, using a base Altering instance as a parameter.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 6:05 PM
  • User8761146 posted

    Guys, but "as OwnedAltering" for Altering is nor gonna work! It always returns null.


    Wednesday, February 24, 2010 12:37 AM
  • User-1149223197 posted

    Guys, but "as OwnedAltering" for Altering is nor gonna work! It always returns null.

    That is because the actual instances cannot be cast down the OwnedAltering types. It sounds like you need to create OwnedAltering objects using the base Altering types, just adding a new property. For this, you should create a static AlteringFactory class to construct these for you. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2010 5:33 AM