3

I have my main form. Form_Main

It creates two instances of two classes.

Candle_Data : TCandle_Data;
Indicator_2700 : TIndicator_2700;

In order for Indicator_2700 to properly compute its values it must have access to the candle data in the obect Candle_Data from inside one of its methods. Thus how can Indicator_2700 access data inside Candle_Data? Does Form_Main have to pass it as a argument at Constructor time?

Both Class declarations are in their own unit file.

3 Answers 3

2

You could use any of the following (non-exhaustive) methods:

  1. Pass the object reference as a parameter to any methods that need it. Of course you need to get hold of Candle_Data so the suitability of this approach really depends who the caller is.
  2. Pass the Candle_Data object reference to the constructor of the other object and then store it in a private member field.
  3. Make the object reference a public property of the single instance of the main form and access it that way.

We don't really have enough information to advise you which is best but the starting point is always to prefer parameters and local variables over global state.

Sign up to request clarification or add additional context in comments.

Comments

2

TIndicator_2700 could have a property to link it to the instance of TCandle_Data that is relevant to its own instance or you should supply it as an argument to the method that needs to access the data.

You could certainly pass the TCandle_Data instance into the constructor of Indicator_2700, and store a reference within the resulting instance until you needed it.

2 Comments

I think it's just the other way round (TIndicator needs TCandleData) but besides that good answer (+1): a property and constructor injection is the obvious solution.
Good spot! I'll modify the answer so-as not to confuse.
0

Both class declarations are in their own unit file.

That suggests that both have nothing to do with the other. But still you want one to have knowledge about the other. It sounds like a little design mixup, but that doesn't need to be the case.

There are multiple solutions, here are three of them, each with its own purpose:

  • Place both classes in the same unit, only if both classes have a common theme/subject (e.g. TCar and TAirplane in the unit Transport),
  • Use one unit in the other unit, only if both units represent different subjects, but one may depend on the other (e.g. unit Transport uses unit Fuel: TCar needs TDiesel, but TDiesel doesn't need a TCar). This only works one-way. Delphi prevents using in both ways with a compiler error: "Circular unit reference to 'Fuel'". The only workaround is to use the second unit in the implementation section, but that usually is considered a big nono.
  • Declare a new base-class in a new unit, only if the base-class has a common subject, but the final descendants do not (e.g. TFuel is used by all transportation classes like TCar, TAirplane and THorse, but TFood (a descendant of TFuel) is only used by THorse and TPerson).

As for how to link both classes together, see the already given answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.