PiedSoftware 3 Posted 20 hours ago In Delphi in System.Class we have this declaration: TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble, ssTouch, ssPen, ssCommand, ssHorizontal); Given that declaration, how can I declare a variable that will be just one of those values? Share this post Link to post
Dmitry Arefiev 103 Posted 19 hours ago var s: ssShift .. ssHorizontal; or upgrade to Delphi 12 and use TShiftStateItem. Share this post Link to post
havrlisan 25 Posted 18 hours ago If you're using a Delphi version 10.3 or newer, you can also achieve this by declaring an inline variable: var LEnum := ssShift; Share this post Link to post
Remy Lebeau 1413 Posted 18 hours ago (edited) 1 hour ago, PiedSoftware said: Given that declaration, how can I declare a variable that will be just one of those values? Prior to Delphi 12, the Enum type for TShiftState is anonymous, so you can't declare a variable of that type, at least not directly. With the introduction of Inline Variables and Type Inference in Delphi 10.3, you can do so, but only if you initialize the variable at the same time you declare it, eg: var state := ssShift; This is exactly the kind of situation why several such Sets in the RTL have been re-written in recent versions to separate their Enum types from their Set types. TShiftState was re-written in Delphi 12, it now looks like this: TShiftStateItem = (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble, ssTouch, ssPen, ssCommand, ssHorizontal); TShiftState = set of TShiftStateItem; So, you can now declare a variable of type TShiftStateItem. Edited 18 hours ago by Remy Lebeau 4 Share this post Link to post
Rollo62 538 Posted 11 hours ago (edited) Edit: Ok, ok: procedure HappyAssignmentsFromHappyDeclarations; var stateOneItem : TShiftStateItem; statesetOneItem : TShiftState; begin // A single item stateOneItem := ssShift; // A set with a single item only statesetOneItem := [ ssShift ]; end; Edited 10 hours ago by Rollo62 Share this post Link to post
Anders Melander 1795 Posted 11 hours ago 6 minutes ago, Rollo62 said: Sorry, what are you looking for? // A single item var state : TShiftStateItem = ssShift; // or // A set with a single item only var state : TShiftState = [ ssShift ]; That's assignments. OP is asking for a type declaration. Share this post Link to post
Rollo62 538 Posted 11 hours ago Yes, I wanted to clarify WHAT exactly he wants to assign ... Share this post Link to post
Lajos Juhász 293 Posted 11 hours ago 29 minutes ago, Rollo62 said: Sorry, what are you looking for? OP is on older version of Delphi 10.4 your example is Delphi 12. Share this post Link to post