Sorry for the poor title,I’m new to OOP so I don’t know what is the term for what I need to do.
I have, say, 10 different Objects that inherit one Object.They have different amount and type of class members,but all of them have one property in common – Visible.
type TSObject=class(TObject);
protected
Visible:boolean;
end;
type
TObj1=class(TSObject)
private
a:integer;
...(More members)
end;
TObj2=class(TSObject)
private
b:String;
...(More members)
end;
...(Other 8 objects)
For each of them I have a variable.
var Obj1:TObj1;
Obj2:TObj2;
Obj3:TObj3;
....(Other 7 objects)
Rule 1: Only one object can be initialized at a time(others have to be freed) to be visible.
For this rule I have a global variable
var CurrentVisibleObj:TSObject; //Because they all inherit TSObject
Finally there is a procedure that changes visibility.
procedure ChangeObjVisibility(newObj:TSObject);
begin
CurrentVisibleObj.Free; //Free the old object
CurrentVisibleObj:=newObj; //assign the new object
CurrentVisibleObj:= ??? //Create new object
CurrentVisibleObj.Visible:=true; //Set visibility to new object
end;
There is my problem,I don’t know how to initialize it,because the derived class is unknown(TObj1,TObj2,Tobj3…Which one?).
How do I do this?
I simplified the explanation,in the project there are TFrames each having different controls and I have to set visible/not visible the same way(By leaving only one frame initialized).
Sorry again for the title,I’m very new to OOP.
One of the first problem here is that you seem to assume you can pass an uninitialized variable to ChangeObjVisibility.
Here, if Obj3 is nil(or worse, a dangling pointer), ChangeObjVisibility has no way to know what is the type of the object it needs to create.
One of the way you could get the class of frame to create is with an array of const, or a function with a case.
That will work if the frame doesn’t need any kind of special initialization.
Next, you could have a call like this one :
Here, SetCurrentFrame replace you ChangeObjVisibility(What you really do here is change the current frame, changing the visibility is just a “side effect”)