[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

C# Inheritance

Hi,

I asked a question on inhertiance, please see the link. Previous C# Inheritance Question

When you create an object of type Calculation - it inherits the methods and properties of its parent.

That makes sense. My question is I have two security objects that are completley seperate from each other, which is fine. However each security object may contain more than one Calculation object, lets say each security object contains 3 Calculation objects. How do I intialise the 3 Calculation objects so that they all reference the same List<Prices> in the Security object? Do I need to define the List<Prices> as a static field?

I have attached a diagram which is probably not that useful as I thnk about it!

Any help would be great.

Thanks
M
0
mcs26
Asked:
mcs26
  • 2
  • 2
  • 2
  • +1
1 Solution
 
TommySzalapskiCommented:
Set it up so the constructor for the calculation object requires a List<Prices> and pass those in from the constructor of the security object.
0
 
TommySzalapskiCommented:
If you set the List<Prices> as static, then all the calculation objects from both security objects will share them. If that's what you want, then yes. Do that.
0
 
käµfm³d 👽Commented:
Set it up so the constructor for the calculation object requires a List<Prices> and pass those in from the constructor of the security object.
If you do that, though, then I would mark the references as readonly. Within any given Calculation object, any assignment to the internal reference to the List<Prices> would desync that particular Calculation's List<Prices> reference with the other two. Making the reference readonly means that it can only be assigned within the constructor (or field initialization) and can never be assigned to again--it is read-only for the lifetime of the Calculation object.

I have concerns about the static approach, but I can't quite put my finger on them at the moment.

Perhaps if you post a sample class hierarchy then we could help you tweak the code.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
mcs26Author Commented:
Hi Guys,

Thanks for the replies. I have written some code below - its only a breif outline as I haven't written much of it yet as I'm a little stuck. Please note I have changed the Calculation Object to Ind as it makes more sense when reading my code.

 I'm still not understanding how I can intialise 3 Ind objects that reference the same Security object and hence the same List<Price> variable.

To give hopefully some more colour. The security object is representing a currency pair, say EURUSD. The Ind object represents a time frame, say 1 mintue, the next Ind object represents a 5 minute time frame & the last 15 minutes, so its important they share the same price data & I was hoping not to have 3 copies of List<Price> when they could just share one.

Thanks again,
M

namespace Simulator
{

	namespace Securities
	{

		public class Price
		{
			public double Bid, Ask;
			public DateTime dtTime;

		}


		public class Security
		{

			public string Name;
			public List<Price> Prices;
			public Indicators.Ind[] IV;


			void Security(string SecName)
			{

				// does other various things just a simple example

				Name = SecName;

				Prices = GetPriceFromDataBase();	// a method in another class
				
			}

		}


	}


	namespace Indicators
	{
		
		public class Ind : Securities.Security
		{
			public int ID;
			public int LookBack;
		}
		
		void Ind() : Securities.Security(Name)	// didn't like this line last night
		{
			
		
		}

	}

}

Open in new window

0
 
OrcbighterCommented:

That makes sense. My question is I have two security objects that are completely separate from each other, which is fine. However each security object may contain more than one Calculation object, lets say each security object contains 3 Calculation objects. How do I initialise the 3 Calculation objects so that they all reference the same List<Prices> in the Security object? Do I need to define the List<Prices> as a static field?

Perhaps you should not be trying to inherit from Calculation. Perhaps a cleaner solution would be to embed the Calculation class as a member variable of the Security class (or an array of them if you are allowed more than one.
Then use the Constructor(s) of the Security class to ensure the correct initialisation of the Calculation object.
0
 
käµfm³d 👽Commented:
I'm still not understanding how I can intialise 3 Ind objects that reference the same Security object and hence the same List<Price> variable.
I don't think the inheritance you are currently using above would be good for this purpose. Instead, using a method that was mentioned previously, you can pass a Security object via the Ind constructor, marking the private/public member as readonly to ensure that the references stay in sync for the lifetime of the objects.

For example:

namespace Simulator
{
    namespace Securities
    {
        public class Price
        {
            public double Bid, Ask;
            public DateTime dtTime;
        }

        public class Security
        {
            public string Name;
            public List<Price> Prices;
            public Indicators.Ind[] IV;

            void Security(string SecName)
            {
                // does other various things just a simple example

                Name = SecName;

                //Prices = GetPriceFromDataBase();	// a method in another class
            }
        }
    }

    namespace Indicators
    {
        public class Ind
        {
            public int ID;
            public int LookBack;
            public readonly Securities.Security _security;

            public Ind(Securities.Security security)
            {
                this._security = security;
            }
        }
    }
}

Open in new window


...and then in the consuming code:

using System;
using System.Collections.Generic;

namespace ConsoleApplication71
{
    class Program
    {
        static void Main(string[] args)
        {
            Simulator.Securities.Security aSecurityObject = new Simulator.Securities.Security();
            Simulator.Indicators.Ind indicator1 = new Simulator.Indicators.Ind(aSecurityObject);
            Simulator.Indicators.Ind indicator2 = new Simulator.Indicators.Ind(aSecurityObject);
            Simulator.Indicators.Ind indicator3 = new Simulator.Indicators.Ind(aSecurityObject);

            // Now all three Ind objects share the same Security object. Since the internal member is
            //  is readonly, Ind cannot change the reference internally. Each Ind will always point to
            //  the object pointed to by aSecurity
        }
    }
}

Open in new window

0
 
mcs26Author Commented:
Thanks for the clear explantion & code!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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