Recent

Author Topic: calling Same Function from different Unit  (Read 1757 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 516
  • Weitental is a very beautiful garbage depot.
calling Same Function from different Unit
« on: February 22, 2021, 10:40:14 am »
Hello Guys :)

so lately i wrote a Function wich does some changes on a record, and i need the code executed for different Units.

is it possible to call this Function from different Unit's and then check wich unit called the Function ?

Something like this:

Code: Pascal  [Select][+][-]
  1. Function TGlobals.WahlZub(ItemName : String; delof, addto : TListBox) : String;
  2. Begin
  3.    //del
  4.    for N := 0 to delof.Items.Count - 1 do begin
  5.       if(ItemName = delof.Items.Strings[N])then begin
  6.          delof.Items.Delete(N);
  7.          Exit;
  8.       end;
  9.    end;
  10.    //add
  11.    addto.Items.Add(ItemName);
  12.  
  13.    //can i check somehow wich Unit called ?
  14.    UnitXY.MyRecord.values := changed
  15. End;
  16.  

Much Thanks :)

balazsszekely

  • Guest
Re: calling Same Function from different Unit
« Reply #1 on: February 22, 2021, 10:56:53 am »
Add another constant parameter to WahlZub function, like this:

Code: Pascal  [Select][+][-]
  1. Function TGlobals.WahlZub(ItemName : String; delof, addto : TListBox; const AUnitName: String = '') : String;
  2. begin
  3.    //...
  4.    ShowMesssage(AUnitName);
  5. end;

If you call WahlZub from unit1:
Code: Pascal  [Select][+][-]
  1. Globals.WhlZub('aaa', ListBox1, ListBox2, {$I %FILE%});
the program will display "unit1.pas", from unit2 "unit2.pas" and so on.

PS: With this method you don't have to modify all the WahlZub calls, only where is necessary.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: calling Same Function from different Unit
« Reply #2 on: February 22, 2021, 11:41:54 am »
Code: Pascal  [Select][+][-]
  1. Globals.WhlZub('aaa', ListBox1, ListBox2, {$I %FILE%});
the program will display "unit1.pas", from unit2 "unit2.pas" and so on.

There's also %CURRENTROUTINE%, but this was added in 3.2.0 so should be handled carefully if there is any chance that an older compiler will be used.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: calling Same Function from different Unit
« Reply #3 on: February 22, 2021, 12:19:40 pm »
There's also %CURRENTROUTINE%, […]
First I thought of that, too, but {$include %currentRoutine%} does apparently not contain any unit or namespace information, it’s really just the routine’s name.

is it possible to call this Function from different Unit's and then check wich unit called the Function ?
No, it’s not possible. There’s no means to determine the caller’s origin, the unit. Units merely modularize your code, yet it essentially doesn’t make a difference—with respect to the generated machine code—whether you put everything in one file or in separate files. This information is lost during compilation.

Code: Pascal  [Select][+][-]
  1.    []
  2.  
  3.    //can i check somehow wich Unit called ?
  4.    UnitXY.MyRecord.values := changed
  5. End;
  6.  
I see you apparently want to access an identifier that’s been declared in another unit, right? So, passing a string as parameter, as GetMem suggested, wouldn’t be of much help.

You will need to pass MyRecord as a var paremeter in order to modify it from another unit. Then you don’t need to specify an FQI (precede the identifier with the unit’s name).

Or do OOP, objected-oriented programming.
« Last Edit: February 22, 2021, 12:25:42 pm by Kays »
Yours Sincerely
Kai Burghardt

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: calling Same Function from different Unit
« Reply #4 on: February 22, 2021, 12:28:02 pm »
There's also %CURRENTROUTINE%, […]
First I thought of that, too, but {$include %currentRoutine%} does apparently not contain any unit or namespace information, it’s really just the routine’s name.

That's a good point, and he did /explicitly/ say unit. What he meant of course is open to clarification :-)

It might I suppose be possible to expand on this a bit using debugging info embedded in the executable, but I suspect that both the detail and the complexity would be more than OP wants.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: calling Same Function from different Unit
« Reply #5 on: February 22, 2021, 10:58:24 pm »
Hmm
You know, down deep in the base of class land, "Tobject" there is a field "UnitName" for which is suppose to report the unit the class was made in !

 Now I've never employed this but looking at the DOCs it claims it has been so since Delphi 2009 ..

so this means the AddTo.UnitName should report where I was created?

Just thinking out load here  :o
The only true wisdom is knowing you know nothing

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: calling Same Function from different Unit
« Reply #6 on: February 22, 2021, 11:48:26 pm »
I tested jamie's notion and it worked.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. . . .
  4.  
  5. procedure TForm1.Button1Click(Sender: TObject);
  6. begin
  7.   memo1.append(foo(form1));
  8. end;
  9.  
  10. ___________________________________
  11.  
  12. unit Unit2;  
  13.  
  14. . . .
  15.  
  16. function foo(sender : tobject) : string;
  17. begin
  18.   exit(sender.UnitName);
  19. end;

Results in "unit1" being written in the memo. :)

But, if I have
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   memo1.append(foo(sender));
  4. end;

"StdCtrls" is returned...

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: calling Same Function from different Unit
« Reply #7 on: February 22, 2021, 11:54:25 pm »
Hmm
You know, down deep in the base of class land, "Tobject" there is a field "UnitName" for which is suppose to report the unit the class was made in !

 Now I've never employed this but looking at the DOCs it claims it has been so since Delphi 2009 ..

so this means the AddTo.UnitName should report where I was created?

Just thinking out load here  :o

Bloody well right.

And if there are no docs or you cannot find them: there is always the code:


Code: Pascal  [Select][+][-]
  1. TObject = class
  2.        public
  3.        .....
  4.        { new since Delphi 2009 }
  5.           class function UnitName : ansistring;      
  6.        ....
  7.        end;  
  8.  
  9.  

Winni




jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: calling Same Function from different Unit
« Reply #8 on: February 22, 2021, 11:54:42 pm »
It looks like you are using List boxes of what ever. Most controls that I know of have a TAG property... This property is for you to assign what ever value you would like...


 Give it a unique value and you can test …

 Case MyList.Tag of
   …

 ….

The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: calling Same Function from different Unit
« Reply #9 on: February 23, 2021, 09:07:21 am »
so this means the AddTo.UnitName should report where I was created?

Not created, declared. Which is an important distinction.

 

TinyPortal © 2005-2018