bravesofts 6 Posted August 30, 2021 is it possible to create an Enumeration Type at runtime ? Share this post Link to post
Rollo62 536 Posted August 30, 2021 (edited) 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 August 30, 2021 by Rollo62 2 Share this post Link to post
bravesofts 6 Posted August 30, 2021 (edited) 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 August 30, 2021 by bravesofts Share this post Link to post
Guest Posted August 30, 2021 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
bravesofts 6 Posted August 30, 2021 (edited) 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 August 30, 2021 by bravesofts Share this post Link to post
Fr0sT.Brutal 900 Posted August 30, 2021 (edited) 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 August 30, 2021 by Fr0sT.Brutal Share this post Link to post
bravesofts 6 Posted August 30, 2021 (edited) 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 August 30, 2021 by bravesofts Share this post Link to post
Guest Posted August 30, 2021 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
bravesofts 6 Posted August 30, 2021 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
Fr0sT.Brutal 900 Posted August 30, 2021 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
bravesofts 6 Posted August 30, 2021 (edited) 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 August 30, 2021 by bravesofts Share this post Link to post
FPiette 383 Posted August 30, 2021 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>; 1 Share this post Link to post
bravesofts 6 Posted August 30, 2021 (edited) 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 August 30, 2021 by bravesofts Share this post Link to post
Fr0sT.Brutal 900 Posted August 30, 2021 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 Posted August 30, 2021 @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
Rollo62 536 Posted August 30, 2021 (edited) 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 August 30, 2021 by Rollo62 Share this post Link to post
Guest Posted August 30, 2021 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
Pat Foley 51 Posted August 30, 2021 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 Posted September 4, 2021 Sorry, but i cannot refrain. The OQ matches the OP's avatar name perfectly! Share this post Link to post