Jump to content
PiedSoftware

How to refer to a single value from an enumerated type declared in a set type?

Recommended Posts

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

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
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 by Remy Lebeau
  • Like 4

Share this post


Link to post

Edit:

 

Ok, ok: :classic_wacko:

procedure HappyAssignmentsFromHappyDeclarations;
var 
   stateOneItem     : TShiftStateItem;
   statesetOneItem  : TShiftState;   
begin
    
    // A single item
    stateOneItem    :=   ssShift;   
    // A set with a single item only
    statesetOneItem := [ ssShift ];
    
end;

 

Edited by Rollo62

Share this post


Link to post
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

Yes, I wanted to clarify WHAT exactly he wants to assign ...

Share this post


Link to post
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

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

×