Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 712215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:49:25+00:00 2026-05-14T04:49:25+00:00

Sorry for the poor title,I’m new to OOP so I don’t know what is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:49:25+00:00Added an answer on May 14, 2026 at 4:49 am

    One of the first problem here is that you seem to assume you can pass an uninitialized variable to ChangeObjVisibility.

    ChangeObjVisibility(Obj3);
    

    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.

    type
      TSObjectClass = class of TSObject;
    const
      ObjectClasses = array[0..X] of TSObjectClass = (TObj1, TObj2, TObj3, ...)
    
    function GetFrameclass(Index : Integer) : TSObjectClass;
    begin
      Result := ObjectClasses[Index]
    
      OR 
    
      case Index of
        0 : Result := TObj1;
        1 : Result := TObj2;
        (...)
      end;
    end;
    

    That will work if the frame doesn’t need any kind of special initialization.

    Next, you could have a call like this one :

    procedure ChangeCurrentFrame(NewFrameIndex : Integer);
    var FrameClass : TSObjectclass;
        vFrame : TSObject;
    begin
      FrameClass := GetFrameClass(NewFrameIndex);
      if CurrentVisibleObj.ClassType <> FrameClass then
      begin
        vFrame := FrameClass.Create(nil);
        SetCurrentFrame(vFrame);
      end;
    end;
    
    procedure SetCurrentFrame(newObj:TSObject); 
    begin 
      if Assigned(CurrentVisibleObj) then
        CurrentVisibleObj.Free; //Free the old object 
      CurrentVisibleObj:=newObj; //assign the new object 
      if Assigned(CurrentVisibleObj) then
        CurrentVisibleObj.Visible:=true; //Set visibility to new object 
    end; 
    

    Here, SetCurrentFrame replace you ChangeObjVisibility(What you really do here is change the current frame, changing the visibility is just a “side effect”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the poor title, I don't really know what to call this. I
Hello (sorry for the poor title) I have a user control which loads different
Sorry for the poor title guys, but I'm whooped. I have a table as
I apologize for the poor title, I don't know how else to explain it.
Sorry for the poor title. I really have no idea how to describe this
I'm sorry about the title. I know it is rather poor but I wasn't
Sorry for the poor title, can't think of a succinct way of putting this..
Sorry ,my English is poor, I have never speak English after I leave the
@Oded: Sorry to have been poor in my exposition... My input document has a
I need some help (sorry for my poor english). I 'm trying to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.