ashes999
ashes999

Reputation: 10163

Two Classes In Two Units With The Same Class Name in Delphi

I have a case in Delphi such as:

I'm in a class that uses unit A and I want to use a static function from the class One in B; how can I do it?

In C#, I'd write something like:

B.One.SomeProcedure

Or even, I would use using to "rename" one namespace. What can I do in Delphi? Removing the uses for unit A is not an option, nor is renaming one of the two classes.

I'm using Delphi 2007.

Upvotes: 0

Views: 2092

Answers (4)

user497849
user497849

Reputation:

how about TNewClassA = class(UnitA.One) and TNewClassB = class(UnitB.One)?

Upvotes: 2

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108948

What do you mean by "a class that uses"? Maybe you mean "a unit that uses"? If so, you need to add both A and B to the uses clause. You can then distinguish between the two procedures by writing A.One.SomeProcedure or B.One.SomeProcedure. If you just write One.SomeProcedure, the procedure in the unit listed last in the uses clause will be used. [Here I assume that One are classes containing class procedures SomeProcedure. If SomeProcedure is an ordinary procedure of the One class, you need -- of course -- to create an object of this class and use this. You can then do myobj := A.One.Create or myobj := B.One.Create, where var myobj: A.One or var myobj: B.One, respectively.]

(Remember also that each unit contains two uses clauses: one at the beginning of the interface section and one at the beginning of the implementation section. If you use something from unit A at line N, the uses clause containing the unit A needs to be located on a line above N.)

Also notice that in Delphi, the class should be called TOne, with the T prefix. Everyone follows this convention, and it looks odd without it.

Upvotes: 2

Mason Wheeler
Mason Wheeler

Reputation: 84540

You can do exactly the same thing. UnitName.ClassName.Method, just like in C#.

Upvotes: 2

Davita
Davita

Reputation: 9114

You can use the following way: [UnitName].[Function]. For example B.SomeProcedure

Upvotes: 2

Related Questions