Jump to content
bravesofts

is it possible to create an Enumeration Type at runtime ?

Recommended Posts

You mean to define a NEW type, like that

 type 
  TFileTypeRange = ftPDF.. ftXLSX;

I think you can do that in certain local scopes and places, but does this really make sense ?

The real goal of enum's is, when they were shared and used to guarantee correct values between several units, like pre-defined constants.

Edited by Rollo62
  • Like 2

Share this post


Link to post

for ex:
in delphi we can put a button and when user click on it a dialog box popup ask him to type names

i ask if there is a possible way that can let me create an Enumeration type dynamically ?

=====
in another meaning : 
Is Enumeration type can be declared at runtime depending of any input situations (just after knowing fields then declare it dynamically and use it after that like any normal Enum and All that at RunTim) ?

Edited by bravesofts

Share this post


Link to post
Guest
38 minutes ago, bravesofts said:

is it possible to create an Enumeration Type at runtime ?

Yes, it is possible ! 😎

And they are called Object (or Class) descendants.

Share this post


Link to post
15 minutes ago, Kas Ob. said:

Yes, it is possible ! 😎

And they are called Object (or Class) descendants.

could you clarify your reply with a simple and easy example plz?

Edited by bravesofts

Share this post


Link to post

I don't think so. Probably with dirty hacks only (like modifying TypeInfo of a previously declared type).

But if you just want to use custom names, you don't need to declare a new type for this

Edited by Fr0sT.Brutal

Share this post


Link to post
10 minutes ago, Fr0sT.Brutal said:

I don't think so. Probably with dirty hacks only (like modifying TypeInfo of a previously declared type).

But if you just want to use custom names, you don't need to declare a new type for this

yeah i understand the dificulties about this question, (and about my example above about names it was just a random scenario that doesn't matter to me !!) what i care is to talk about my goal subject(Enum Types) and Possible real scenarios will impose the use of a Dynamic Enumeration types ...

=====

thanks Mr Fr0sT.Brutal   for your Logic Reply..

Edited by bravesofts

Share this post


Link to post
Guest
10 minutes ago, bravesofts said:

could you clarify your reply with a sample and easy example plz?

Well there is no specific example, it is OP in abstract, its essence.

 

you "can put a button and when user click on it a dialog box popup ask him to type names", then take that name and associated it with a class that is inherited from your type, put it in a field or a property and you have your runtime type, runtime type that is configured by your user.

Share this post


Link to post
1 minute ago, Kas Ob. said:

Well there is no specific example, it is OP in abstract, its essence.

 

you "can put a button and when user click on it a dialog box popup ask him to type names", then take that name and associated it with a class that is inherited from your type, put it in a field or a property and you have your runtime type, runtime type that is configured by your user.

let says that my scenario is about gathering a range of colors and my user types 3 or 5 colors ( note: the popup box allow user adding whatever colors he want without knowing the limit count until the popup box closed by the user) ofcourse we can configure this popupbox with a Max Values....

-----------------

what i need to know is how could you know the count of colors that the user entered and  put it in a predefined field or a property !!!!

Share this post


Link to post
3 minutes ago, bravesofts said:

let says that my scenario is about gathering a range of colors and my user types 3 or 5 colors ( note: the popup box allow user adding whatever colors he want without knowing the limit count until the popup box closed by the user) ofcourse we can configure this popupbox with a Max Values....

Why do you think you need dynamic enum here? The task looks like "get a user-picked set of items from colors enum". Of course there could be not more than 256 elements in enum so if you allow custom colors you'd have to use arrays/lists of integers.

Share this post


Link to post
7 minutes ago, Fr0sT.Brutal said:

Why do you think you need dynamic enum here? The task looks like "get a user-picked set of items from colors enum". Of course there could be not more than 256 elements in enum so if you allow custom colors you'd have to use arrays/lists of integers.

let think that delphi has just Enum Types for just a second and think about my Random Scenario Above is impose us to use just the Enum Type !!!!

----------

i know that scenario it wasn't able to convincing you to use the Enum Type (and me too)

but i want to know just if delphi has dynamic (Enum Types )

Edited by bravesofts

Share this post


Link to post
9 minutes ago, bravesofts said:

let says that my scenario is about gathering a range of colors and my user types 3 or 5 colors ( note: the popup box allow user adding whatever colors he want without knowing the limit count until the popup box closed by the user) of course we can configure this popupbox with a Max Values....

 

what i need to know is how could you know the count of colors that the user entered and  put it in a predefined field or a property !!!!

A list is a good solution for that. Either a list of strings, or a list or colors or a list of color index. The best depends on what you need to do with that data after having captured it. Example:

var ColorList : TList<TColor>;

 

  • Like 1

Share this post


Link to post

while EnumTypes was just a const list of predefined values ... 

i think it is impossible or a kind of crazy to make a dynamic declaration for it !!!!

same thing with any const value that we use in our Apps ...

--------

my conclusion:

An Enumeration Type is just  a predefined const list of values

-------

Answer is no way logically and not unfortunatelly 

Edited by bravesofts

Share this post


Link to post
11 minutes ago, bravesofts said:

i know that scenario it wasn't able to convincing you to use the Enum Type (and me too)

but i want to know just if delphi has dynamic (Enum Types )

Well, OK. Talking precisely, if you ask if it's possible to create dynamic enumeration type with random props - then answer is: No

But - depending on what you want from this dynamic enum some kind of imitation is possible

Share this post


Link to post
Guest

@bravesofts Lets take a step back and look at the full picture, we need to find a clear question about what is enumerated types In Pascal.

 

The answer is simple, they merely are constants with there own scope, meaning they are constants but declared in a way to be only used in specific places, and that is it, also their highest target in their life is to be used in an if statement or a case statement, right ?

 

When you look at them like that then the following will start to make sense

type
  TMyTypeAbs = Integer;
  //TMyTypeAbs = class;
  //TMyTypeAbs = TColor;
  //TMyTypeAbs = TMyComplexColorRecord;

  TMyDynamicTypeEnum = class
  private
    FList: array of TMyTypeAbs;
    procedure GetType(Index: Integer; const Value: TMyTypeAbs);
    //function SetType(Index: Integer): TMyTypeAbs;
  public
    destructor Destroy; overload;
    procedure RergisterType(Color: TMyTypeAbs);
    procedure UnRergisterType(Color: TMyTypeAbs);
    property Enum[Index: Integer]: TMyTypeAbs {read SetType} write GetType;
  end;

now you can go fancy with it adding class functions, make their holder a Singletone, mix of that....

 

Share this post


Link to post

This is a class, not an enum.

I think the advantage of enum ist, that you can use a "speakable name" to identify a numeric value.

If you enhance this class to store also the name of an enum, you get closer,

but you still cannot simply use "AVariable := TMyDynamicEnumType.FancyEnumName; ", I think this is what bravesofts is asking for.

 

Ok, you could use specific class properties as names for each enum, but still hard to dynamically enhance that.

Edited by Rollo62

Share this post


Link to post
Guest
12 minutes ago, Rollo62 said:

but you still cannot simply use "AVariable := TMyDynamicEnumType.FancyEnumName; ", I think this is what bravesofts is asking for.

OK, Will this do ? or is it better a little ?

 

  TMyDynamicTypeEnum = class
  private
    FList: array of TMyTypeAbs;
    function GetValue(const Name: string): TMyTypeAbs;
  public
    destructor Destroy; overload;
    procedure RergisterType(Color: TMyTypeAbs);
    procedure UnRergisterType(Color: TMyTypeAbs);
    property Enum[const Name: string]: TMyTypeAbs read GetValue; default;
  end;


..
var
  T: TMyTypeAbs;
  DynamicType: TMyDynamicTypeEnum;
begin
  T := DynamicType['Test'];
end;

 

Share this post


Link to post
3 hours ago, bravesofts said:

my conclusion:

An Enumeration Type is just  a predefined const list of values

 

You could say the same about clYellow, clMoneyGreen.

 

By setting a couple of adjustable colors 

UserColorBrush: Tcolor;

UsercolorPen: Tcolor; 

begin
  ColorDialog1.Options := [cdFullOpen,cdShowHelp];
  if ColorDialog1.Execute then
  begin
    MachineColor:= ColorDialog1.Color;
    EngineSubModel[11]:= inttostr(MachineColor); //for saving loading 

adding these to a list to get a count.    

   

 

 

 

Share this post


Link to post
Guest

Sorry, but i cannot refrain. The OQ matches the OP's avatar name perfectly!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×