Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 733
  • Last Modified:

c# inheritance

Hi,

I have two classes. One called Security and another called Calculation.

The class Calculation inherits from the Security class. The Security class contains a public type of List<Prices>. When the Security Class is intialised the List<Prices> type is populated, lets say with a 100 elements. It also creates a Calcualtion object and then calls a method in the Calculation class.

My question is the Calculation class can see the public field type of List<Prices> but when stepping through the code the List<Prices> is empty and not conatining the 100 elements that it should be?

I know it is possible to pass the List<Prices> as a parameter to the method in the Calculation Class but I am trying to learn about inheritance.

Thanks for any help,
M
0
mcs26
Asked:
mcs26
  • 2
1 Solution
 
Julian HansenCommented:
When the Security Class is intialised the List<Prices> type is populated, lets say with a 100 elements. It also creates a Calcualtion object and then calls a method in the Calculation class.
This is not inheritance.

Inheritance is when you define Calculation as inheriting from Security and then instantiate Calculation (not Security) - which then inherits the prices value from the parent.

What you are doing is creating a Security object which is then creating another (new) object of type calculation which is now effecitvely a local variable to security but not linked to the actual object that created it - the new object will have its own copy of prices.

So
class Security 
{ 
  List<Prices> prices;
  void Security()
  {
     // populate prices
     // instantiate Calculation
  }
}
class Calculatation : Security
{
}

...

Security sec = new Security();

Open in new window

This results in two objects

The first is the security object with the initialised Prices list
The second object is the Calculation object created by the Security object.

While Calculation is inheriting from Security - the Calculation constructor was called on the second object so the list was not initialised. I am assuming you are not invoking the Security constructor in Calculation's constructor - as this would probably work - but still be the wrong way of doing this.

Inheritiance works like this

When you create an object of type Calculation - it inherits the methods and properties of its parent. If you don't speciy a constructor the parents constructor will be invoked and any initialisation in the parent will happen autoamtically. If you define a constructor in the child class it will need to explicitly invoke the parent constructor in order to perform any initialisation defined there.

Like so
class Security 
{ 
  List<Prices> prices;
  void Security()
  {
     // populate prices
     // REMOVE instantiate Calculation
  }
}
class Calculatation : Security
{
   void Calculationy() : Secuirty()
   {
      
   }
}

...

Security sec = new Security();

Open in new window


If I understand you correctly and without seeing code the above should fix the problem.
0
 
mcs26Author Commented:
Great thanks for the detailed explanation, very easy to follow!
0
 
Julian HansenCommented:
You are welcome
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.

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