Reputation: 674
I have types in distincts units with the same name and I have the unit name in a string. I need to access the specific type of this unit. How do I do that?
Example:
unit Unit1
type
TFooType = (
bar1,
bar2
);
then, I have another unit
unit Unit2
type
TFooType = (
foo1,
foo2,
foo3
);
And, somewhere in my code I have the a string variable "UnitName" with the value 'Unit1' within it and I want to access the Unit1's "TFooType" type by the variable.
I'm using Delphi 2007
Sorry for my bad english.
Thanks in advance.
Upvotes: 0
Views: 519
Reputation: 2683
@Hrukai, Just like with lego, there's a lot you can do, but somethings just weren't designed to be used that way.
Sounds to me like your end goal is to access the type, and your starting point is a variable name. Had you implemented your variables as classes (OOP), you could simplly do Obj.ClassName to find its type... but also, had you chosen classes for your implementation, I predict this need (to access the type from the variable) would have never arisen in the first place.
Resist the urge to create a new pattern, and instead exploit the power of classes. http://www.delphibasics.co.uk/Article.asp?Name=OOExample
Upvotes: 1
Reputation: 612963
The best you could do would be something like if name='Unit1' then T := Unit1.TFoo
etc. But what can you do with T
anyway? Since the enumerated types from the different units are different, it's hard to imagine being able to anything with T
. In fact how would you even define T
? The only thing I could imagine would be possible would be to return the type info but I'm letting my imagination run wild now!
Upvotes: 0
Reputation: 163287
You can't choose which units to include at run time. Units are a compile-time concept.
Furthermore, your two types, despite having the same base name, are completely distinct types. Elsewhere in your code, you cannot have a variable of type TFooType
and arbitrarily decide whether to assign it values from both of those units. The variable can only hold values from one type.
You're going to have to think of some other way of accomplishing your real task. I invite you to post a new question describing what your real task is.
Upvotes: 11