Ian Branch 128 Posted February 25, 2022 Hi Team, Let's say I have two units in the App. Unit1 & Unit 2 where Unit1 is the main unit and Unit2 is a datamodule with a ttable, MyTable, on it. Is it possible to create an alias for the datamodule/ttable? i.e. instead of having to type 'Unit2.Mytable.FieldByName......' when calling it from Unit1, instead, something like 'U2MT.FieldByName.....'? Would save a hell of a lot of typing... :-) Or, is there an alternative?? Whilst I have been specific here in regards to the units/components, I can see additional usage if the ability exists. Regards & TIA, Ian Share this post Link to post
Guest Posted February 25, 2022 (edited) for a class, you can create a alias that way: type TMyClassOriginal = class public function HelloWorld: string; end; TMyAlias = TMyClassOriginal; procedure TForm1.Button1Click(Sender: TObject); var MyAlias:TMyAlias; begin MyAlias:=TMyAlias.Create; // TMyClassOriginal.Create; // it's valid too! try ShowMessage( MyAlias.HelloWorld ); finally MyAlias.Free; end; end; { TMyClassOriginal } function TMyClassOriginal.HelloWorld: string; begin result := 'Hello cruel world'; end; now, to units (namespaces... not necessaraly), that's why there is the keyword "uses". That way, whatever the classes or other objects within a unit, you no longer need to indicate "where the object is contained"... the only case, I think, is when there are two objects with the same name or some particularity to each. A very common case in Delphi is some enumerates that were not created with the "{ENUMSCOPED ON}" compiler directive and can cause unexpected behavior when in use. Edited February 25, 2022 by Guest Share this post Link to post
Pat Foley 52 Posted February 25, 2022 56 minutes ago, Ian Branch said: Is it possible to create an alias for the datamodule/ttable? i.e. instead of having to type 'Unit2.Mytable.FieldByName......' when calling it from Unit1, instead, something like 'U2MT.FieldByName.....'? Better to use reference... { Once instance is created you use references to it.. procedure TForm5.Button1Click(Sender: TObject); var sourceDM: dm5.TDataModule6; dmLog: TStrings; begin dmLog := sourceDM.dm6log; //or just dmLog := dm5.TDataModule6 ... //in dm5's TDataModule6 has stringlist for logging dm6log: Tstrings; Share this post Link to post
Guest Posted February 25, 2022 non-unnecessary say that: we only use "namespaces... a.k.a lib.section.classes.TMyClass" when exist "a possible conflict" on place! for rest, dont try this! it's unreadable! use "USES" uses uMyLib.MySection.MyClass; ... var MyValue: TMyClass; // into "uMyLib.MySection.MyClass" begin ... MyValue := TMyClass.Create; ... Share this post Link to post
Ian Branch 128 Posted February 25, 2022 Hi All, Sorry, you are doing things that are over my head. 😞 I'm sure what you are telling me is quite correct, but I'm afraid I don't understand how to apply them to my scenario. That is my knowledge deficiency. I really would appreciate it if you could explain in terms of my Unit1/Unit2/TTable example. Then perhaps I can relate it to what you have above and reach an understanding. Regards, Ian Share this post Link to post
Pat Foley 52 Posted February 25, 2022 How about mainunit and unit9 listing the DataModule named DM in this example. Button1Click in mainunit calls buildEmailList that has two tables as arguments. Note in button1click the tables used are in DM. in the buildEmaillList procedure only tables passed are used. Button2Click should get you started. 🙂 //unit9 //... // TDM = class(TDataModule) // MyTable: TFDMemTable; // emailsTable: TFDMemTable; Uses unit9; procedure TForm8.buildEmailList(SourceTable, EmailTable: TfdmemTable); begin EmailTable.Open; SourceTable.Close; end; procedure TForm8.Button1Click(Sender: TObject); begin buildEmailList(DM.MyTable, DM.emailsTable); end; procedure TForm8.Button2Click(Sender: TObject); var bigtable: TFDMemTable; EmailsNeedtoSend: TFDMemTable; begin bigtable := DM.MyTable; EmailsNeedtoSend := DM.emailsTable; end; Share this post Link to post
Ian Branch 128 Posted February 25, 2022 (edited) Hi Pat, Ahhh. Now I see/understand. Thank you for the clarification. Much Appreciated. Thank you to all contributors. So, there is no such thing as a freebee. What, if any, is the penalty for this construct? Regards, Ian Edited February 25, 2022 by Ian Branch Share this post Link to post
Uwe Raabe 2064 Posted February 25, 2022 7 hours ago, Ian Branch said: Is it possible to create an alias for the datamodule/ttable? i.e. instead of having to type 'Unit2.Mytable.FieldByName......' when calling it from Unit1, instead, something like 'U2MT.FieldByName.....'? Note that Unit2.Mytable.FieldByName will not work because it misses the datamodule where MyTable is declared. Sticking to the concrete example and assuming that the datamodule in Unit2 is named DataModule2, declare a public function in Unit2 like this: function U2MT: TTable; begin Result := DataModule2.MyTable; end; Now in Unit1 and everywhere else you can write things like U2MT.FieldByName as long as Unit2 is in the uses clause. Share this post Link to post