* * *

Author Topic: How Unit2 procedure result show in Main Unit1 form????  (Read 419 times)

fiazhnd

  • Newbie
  • Posts: 4
How Unit2 procedure result show in Main Unit1 form????
« on: May 18, 2017, 10:10:42 am »
My Code is becoming big so i decide to spread over different units so easy to read and update,
Unit1 is my main page where i created the form and object, i have issue the result of 'A' want to show in Edit1 which is on Unit1 so compiler give error when compiler " Unknown identifier or variable is not declared: 'Edit1'."

What i need to do so 'v4' result show in Edit1 which in Unit1?

Unit1
Code: Pascal  [Select]
  1. procedure ReadClick(Sender: TObject);
  2. begin
  3. if ComboBox2.Text = ('A') then begin ImmoCodeRead('A') end;
  4.  
  5. end;

Unit2
Code: Pascal  [Select]
  1. Procedure ImmoCodeRead(logic: string);  
  2. begin
  3. if logic = ('A') then begin
  4.     v1:=(inttohex(GetByteHexEdit($60),2)+inttohex(GetByteHexEdit($61),2)+
  5.          inttohex(GetByteHexEdit($62),2)+inttohex(GetByteHexEdit($63),2));
  6.     v2:= StrToInt('$'+v1) xor $FABE8675
  7.     v3:= IntToHex(v2, 8);
  8.     v4:= chr(StrToInt('$'+v3[1]+v3[2]))+chr(StrToInt('$'+v3[3]+v3[4]))
  9.         +chr(StrToInt('$'+v3[5]+v3[6]))+chr(StrToInt('$'+v3[7]+v3[8]));
  10.  
  11.     Edit1.Text:= (v4);
  12.  
  13. end;
« Last Edit: May 18, 2017, 10:12:25 am by fiazhnd »

Zoran

  • Hero Member
  • *****
  • Posts: 1131
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How Unit2 procedure result show in Main Unit1 form????
« Reply #1 on: May 18, 2017, 12:22:38 pm »
One way would be:

Unit1:
Code: Pascal  [Select]
  1. procedure ReadClick(Sender: TObject);
  2. begin
  3.   if ComboBox2.Text = ('A') then
  4.     Edit1.Text := ImmoCodeRead('A');
  5.  
  6. end;

Unit2:
Code: Pascal  [Select]
  1. function ImmoCodeRead(logic: string): String;
  2. begin
  3.   Result := '';
  4.   if logic = ('A') then begin
  5.     v1:=(inttohex(GetByteHexEdit($60),2)+inttohex(GetByteHexEdit($61),2)+
  6.          inttohex(GetByteHexEdit($62),2)+inttohex(GetByteHexEdit($63),2));
  7.     v2:= StrToInt('$'+v1) xor $FABE8675
  8.     v3:= IntToHex(v2, 8);
  9.     v4:= chr(StrToInt('$'+v3[1]+v3[2]))+chr(StrToInt('$'+v3[3]+v3[4]))
  10.         +chr(StrToInt('$'+v3[5]+v3[6]))+chr(StrToInt('$'+v3[7]+v3[8]));
  11.  
  12.     Result := v4;
  13.   end;
  14. end;

fiazhnd

  • Newbie
  • Posts: 4
Re: How Unit2 procedure result show in Main Unit1 form????
« Reply #2 on: May 18, 2017, 12:54:26 pm »
Thank you Zoran, It work like charm and it save me from a lot of work..................

i was also working same type of syntax structure after i post here but one thing ( Result := ' ';) is in your code can you explain to me this?

One question is more what if i have multiple result need to show in multiple edit object?
« Last Edit: May 18, 2017, 01:01:33 pm by fiazhnd »

Zoran

  • Hero Member
  • *****
  • Posts: 1131
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How Unit2 procedure result show in Main Unit1 form????
« Reply #3 on: May 18, 2017, 03:07:02 pm »
Thank you Zoran, It work like charm and it save me from a lot of work..................

i was also working same type of syntax structure after i post here but one thing ( Result := ' ';) is in your code can you explain to me this?

Your function should return something if logic <> 'A'

One question is more what if i have multiple result need to show in multiple edit object?

You can use var or out parameters to return multiple results and then assign these to edits.

https://www.freepascal.org/docs-html/ref/refsu65.html
https://www.freepascal.org/docs-html/ref/refsu66.html
https://www.thoughtco.com/return-multiple-values-from-delphi-function-1057664
http://wiki.freepascal.org/Variable_parameter

fiazhnd

  • Newbie
  • Posts: 4
Re: How Unit2 procedure result show in Main Unit1 form????
« Reply #4 on: May 18, 2017, 05:47:07 pm »
No luck with Var and Out parameter i know something wrong  >:(

Code: Pascal  [Select]
  1. procedure ReadClick(Sender: TObject);
  2. begin
  3.   if ComboBox2.Text = ('A') then begin ImmoCodeRead('B');
  4. Edit1.Text := ImmoCodeRead(v4);                                        
  5. Edit1.Text := ImmoCodeRead(vin);
  6. end;
  7. end;
  8.  

Code: Pascal  [Select]
  1. function ImmoCodeRead(const logic: string out v4,Vin:String): String;
  2. begin
  3.   Result := '';
  4.   if logic = ('B') then begin
  5.     v1:=(inttohex(GetByteHexEdit($60),2)+inttohex(GetByteHexEdit($61),2)+
  6.          inttohex(GetByteHexEdit($62),2)+inttohex(GetByteHexEdit($63),2));
  7.     v2:= StrToInt('$'+v1) xor $FABE8675
  8.     v3:= IntToHex(v2, 8);
  9.     v4:= chr(StrToInt('$'+v3[1]+v3[2]))+chr(StrToInt('$'+v3[3]+v3[4]))
  10.         +chr(StrToInt('$'+v3[5]+v3[6]))+chr(StrToInt('$'+v3[7]+v3[8]));
  11.  
  12.   Vin := '';
  13.    Address := $50
  14.    While Address <> $61 do Begin
  15.    Vin := Vin + chr(GetByteHexEdit(Address));
  16.    Address := Address +1 ;
  17.    end;
  18.  
  19. Result := v4;
  20. Result := Vin;  
  21. end;
  22. end;
  23.  

molly

  • Hero Member
  • *****
  • Posts: 1568
Re: How Unit2 procedure result show in Main Unit1 form????
« Reply #5 on: May 18, 2017, 06:19:02 pm »
Try:
Code: Pascal  [Select]
  1. procedure ReadClick(Sender: TObject);
  2. var
  3.   V4, VIn: String;
  4. begin
  5.   if ComboBox2.Text = ('A') then
  6.   begin
  7.     ImmoCodeRead('B', V4, VIn);
  8.  
  9.     Edit1.Text := v4;  
  10. // In your original example/question you only wanted to have V4 displayed in Edit1.Text, now you are overwriting that text in Edit1 with vIn, in case you uncomment the next line.                                    
  11. //    Edit1.Text := vin;
  12.   end;
  13. end;
  14.  

And there's more going wrong there. Seems you do not know the basics yet.

Code: [Select]
Result := v4;
Result := Vin; 

There is only one result in a function. The others values are returned using their (variable) name. In your specific case for function ImmoCodeRead that would be variables V4 and Vin

The above does not imply you are not allowed to return a value by using the function result and a var/out parameter. Sometimes (as in your case) it seems redundant.

Which bring the question why do you need a function ? e.g. you have to decide what you want to return where exactly first (in the result or var/out parameter) and act accordingly.

BTW: your code makes no sense in whatever setup i know, you are using procedures while they should be procedures of object (at least ReadClick should). And i am guessing that you are messing up your (global) variables quite considerably with defining your parameter variables with the same name.

edit: typo's
edit2: comment
« Last Edit: May 18, 2017, 07:03:38 pm by molly »

fiazhnd

  • Newbie
  • Posts: 4
Re: How Unit2 procedure result show in Main Unit1 form????
« Reply #6 on: May 20, 2017, 07:46:18 am »
Thank you man its work i use var parameter and it return me two values work fine with me now

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus