Recent

Author Topic: SOLVED how to pass a public property from a form to an object?  (Read 2190 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 619
SOLVED how to pass a public property from a form to an object?
« on: November 10, 2018, 09:30:50 am »
I have a form with a public property in a TForm

Code: Pascal  [Select][+][-]
  1. public
  2.   ...
  3.   TFrmMaintainUsers = class(TForm)
  4.   ...
  5.   public
  6.     property RoleName : string  read Get_RoleName write Set_RoleName;
 
   
I create an instance of my class:

Code: Pascal  [Select][+][-]
  1. procedure TFrmMaintainUsers.ButtonInsertClick(Sender: TObject);
  2. var
  3.   CreateUser : TSqliteDbUsers;
  4. begin
  5.   CreateUser := TSqliteDbUsers.Create;
  6.   CreateUser.CreateUser(DBGridUsers);
  7.   CreateUser.Free;
  8. end;
  9.  

The class TSqliteDbUsers :


 
Code: Pascal  [Select][+][-]
  1.  TSqliteDbUsers = class(TObject)
  2.   private
  3.     function Get_RoleName: string;
  4.     procedure Set_RoleName(AValue: string);
  5.     private
  6.       //FRoleName : String;
  7.       //property RoleName : string  read Get_RoleName write Set_RoleName;
  8.   var
  9.  
  10.     test : String; //temp for testing
  11.       ...                                      
  12.  
  13. constructor TSqliteDbUsers.Create;
  14. begin
  15.   test := frmmaintainusers.RoleName;  //this is wrong ?
  16. end;      

There is an SIGSEGV error :
in file formmaintainusers.pas at line 61: Result := FRoleName; 

why can i not acces the public property?
« Last Edit: November 11, 2018, 01:07:36 pm by Hansvb »

ASerge

  • Hero Member
  • *****
  • Posts: 2250
Re: how to pass a public property from a form to an object?
« Reply #1 on: November 10, 2018, 10:00:40 am »
There is an SIGSEGV error :
in file formmaintainusers.pas at line 61: Result := FRoleName; 
frmmaintainusers is nil, or TFrmMaintainUsers.Get_RoleName dereferences the nil pointer.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: how to pass a public property from a form to an object?
« Reply #2 on: November 10, 2018, 11:04:06 am »
Quote
Code: Pascal  [Select][+][-]
  1. constructor TSqliteDbUsers.Create;
  2. begin
  3.   test := frmmaintainusers.RoleName;  //this is wrong ?
  4. end;
I don't know what you want to achieve in detail, but this code fragment rings an alarm: It is never good to tie a class to the existance of the instance of another class just for the purpose to get a value from it. Your code only works when an instance named frmmaintainusers exists; you cannot even rename that without breaking TSqliteDbUsers!

I'd much prefer this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TSqliteDbUsers = class
  3.   private
  4.     FRoleName: String;
  5.   public
  6.     constructor Create(ARoleName: String);
  7.   end;
  8.  
  9. constructor TSqliteDbUsers.Create(ARoleName: String);
  10. begin
  11.   inherited Create;
  12.   FRoleName := ARoleName;
  13. end;
  14.  
  15. ---
  16.  
  17. procedure TFrmMaintainUsers.ButtonInsertClick(Sender: TObject);
  18. var
  19.   CreateUser : TSqliteDbUsers;
  20. begin
  21.   CreateUser := TSqliteDbUsers.Create(RoleName);
  22.   ...
  23. end;
  24.  
« Last Edit: November 10, 2018, 11:08:35 am by wp »

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: how to pass a public property from a form to an object?
« Reply #3 on: November 10, 2018, 11:32:15 am »
Agree with wp.  You should separate your business (the work, process, calculations...) from your interface (windows, dialogs...).  Try to think in a frontend-backend design:
  • frontend gets user input and sends it to backend, then gets result from backend and shows it to the user.
  • backend is what really does the work.
This will make things more easy to write, understand, and maintain, and also to port if needed (i.e. Android interface is way different than PC, so you only need to rewrite the frontend while backend doesn't change at all).
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: how to pass a public property from a form to an object?
« Reply #4 on: November 10, 2018, 02:44:42 pm »
Technically that would work however, I think the ButtonInsertClick code block is getting executed before the return of the
form create, maybe he is doing something in the OnCreate  which would make that invalid..

  This is what I do, because I such things, I have the creation of the class accept a owner which would be pointed to via SELF.
  That owner can then be logged internally to point to the instance of the main form it lives in and then the main class can be
used to cast over it to access the members of the main form.

  I do such code myself so it's not like a terrible thing to do :D
The only true wisdom is knowing you know nothing

Hansvb

  • Hero Member
  • *****
  • Posts: 619
Re: SOLVED how to pass a public property from a form to an object?
« Reply #5 on: November 11, 2018, 01:08:11 pm »
Thanks wp was right.

 

TinyPortal © 2005-2018