Jump to content
PiedSoftware

Why is an enumerated type requiring the type name before the constants?

Recommended Posts

Hi, I was surprised when a simple bit of code did not compile for me. I am using Delphi 10.1, and TEditCharCase is defined in System.UITypes thus:
 

  TEditCharCase = (ecNormal, ecUpperCase, ecLowerCase);

I have a property of that type in a class TMapItem declared thus:
 

property CharCase: TEditCharCase read fCharCase write fCharCase;

Control clicking on TEditCharCase takes me straight to that line in UITypes.
But when I wrote this innocent looking code:          

 case mapItem.CharCase of
    ecNormal:    expr := value;
    ecUpperCase: expr := value.ToUpper;
    ecLowerCase: expr := value.ToLower;
end;

it refused to compile, saying "E2003 Undeclared identifier: 'ecLowerCase'", etc for the 3 enumerated constants.

But I got it to compile by prefixing the constants with the type name, thus:

 case mapItem.CharCase of
   TEditCharCase.ecNormal:    expr := value;
   TEditCharCase.ecUpperCase: expr := value.ToUpper;
   TEditCharCase.ecLowerCase: expr := value.ToLower;
 end;


This is strange. There are lots of places of enumerated types that don't need to have the type prefixed. 

Can anyone explain this?

TIA
Mark Patterson

Share this post


Link to post
Guest

when occurr the "conflict" between names (any object/vars etc...) it's necessary identify "where it from came"  (what unit, what record, etc...) the value, then, you need use a "NameSpace" for any divergence!

For that, it's necessary always pay attention when "give a name" for a object/var etc...

You can find some examples in source code from RAD!

 

for example:

Quote

if unitMyRecords.mapItem.CharChase = expr  then...

 

other thing: here you're already using a "namespace" way:  using "case

 case mapItem.CharCase of
   TEditCharCase.ecNormal
...

hug

Edited by Guest

Share this post


Link to post

I noticed in the library source code that some of the uses of TEditCharChase constants were not prefixed with the type. For example, in DbCtrls there is this:
 

        case CharCase of
          ecUpperCase: S := AnsiUpperCase(S);
          ecLowerCase: S := AnsiLowerCase(S);
        end;

When you ctrl-click the constants there you are sent to a line in StdCtrls that is part of this sequence of declarations:

const
{ Editors common support }
  ecNormal	= System.UITypes.TEditCharCase.ecNormal;
  ecUpperCase	= System.UITypes.TEditCharCase.ecUpperCase;
  ecLowerCase	= System.UITypes.TEditCharCase.ecLowerCase;

type
  TEditCharCase = System.UITypes.TEditCharCase;


So, some people seem to want to work around the SCOPEDENUMS.

  • Like 1

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

×