PiedSoftware 3 Posted December 4 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 105 Posted December 4 var s: ssShift .. ssHorizontal; or upgrade to Delphi 12 and use TShiftStateItem. Share this post Link to post
havrlisan 25 Posted December 4 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 1429 Posted December 4 (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 December 4 by Remy Lebeau 4 Share this post Link to post
Rollo62 538 Posted December 4 (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 December 4 by Rollo62 Share this post Link to post
Anders Melander 1815 Posted December 4 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 December 4 Yes, I wanted to clarify WHAT exactly he wants to assign ... Share this post Link to post
Lajos Juhász 295 Posted December 4 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