Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Using enumeration names in case statements
Thu, Aug 13 2015 7:36 AMPermanent Link

Michael Dreher

Hi,
according to the help:

 // Internally, enumerations are handled as integers by the compiler, and you can cast enumerations  
 // as integers and integers as enumerations.

When I use enumeration names as an expression in the case statement, the application starts blank. Example:

type
 TTestInstructionDataType = (
   TIDT_INT,
   TIDT_FLOAT,
   TIDT_BOOL
 );
  TForm1 = class(TForm)
     Label1: TLabel;
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
  private
  public
  end;
var
  Form1: TForm1;
implementation

procedure TForm1.Button1Click(Sender: TObject);      
var i : integer;
begin           
 i := 1;

//(a)
 case i of
   TIDT_INT : Label1.Caption := '1' ;
   TIDT_FLOAT : Label1.Caption := '2' ;
   TIDT_BOOL : Label1.Caption := '3' ;
 end;  

//(b)
 case i of
   0 : Label1.Caption := '1' ;
   1 : Label1.Caption := '2' ;
   2 : Label1.Caption := '3' ;
 end;
end;
end.        

The code compiles without errors. Because the case statement (a) is in there, the browser application
starts (IDE or direct in a browser with an external server) with a blank page, meaning the TLable and the TButton are not visible.

If I comment out (a), the applicartion starts normal, and (b) is an alternative (working) case statement. Of course I want to use symbolic names instead of integer literals.

I've tried explicite casting:

 case i of
   integer(TIDT_INT) : Label1.Caption := '1' ;
    // ...
  end;

but then the compiler informs "expect const value but found integer". So TIDT_INT is not a constant value? How to do it correct?

Michael Dreher
Thu, Aug 13 2015 8:01 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< When I use enumeration names as an expression in the case statement, the application starts blank. Example: >>

I don't think that's the cause.  EWB's RTL and component library uses enumerations in case statements *everywhere*.  I just tried this to be sure:

procedure TForm1.Button14Click(Sender: TObject);
var
  I: Integer;
begin
  I:=1;
  case I of
     crAuto:
        ShowMessage('Auto');
     crCrossHair:
        ShowMessage('Cross');
     crDefault:
        ShowMessage('Default');
     else
        ShowMessage('else');
     end;
end;

and it works fine.

Can you send me an example project that demonstrates the issue ?

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Aug 14 2015 1:28 AMPermanent Link

Michael Dreher

Tim,

 // Can you send me an example project that demonstrates the issue ?

I noticed the JavaScript output I send you is incomplete.

unit1_tform1.$p.tform1_button1click = function(sender)
{
  var $t = this, i;
  i = 1;
  if (i == )    < -------
     $t.tform1_label1.tlabelcontrol_setcaption("1");
  else if (i == )   < -------
     $t.tform1_label1.tlabelcontrol_setcaption("2");
  else if (i == )   < -------
     $t.tform1_label1.tlabelcontrol_setcaption("3");
 // ....
};

Michael Dreher
Fri, Aug 14 2015 9:27 AMPermanent Link

Matthew Jones

On 14/08/2015 06:28, Michael Dreher wrote:
> Tim,
>
>    // Can you send me an example project that demonstrates the issue ?
>
> I noticed the JavaScript output I send you is incomplete.
>
> unit1_tform1.$p.tform1_button1click = function(sender)
> {
>     var $t = this, i;
>     i = 1;
>     if (i == )    < -------
>        $t.tform1_label1.tlabelcontrol_setcaption("1");
>     else if (i == )   < -------
>        $t.tform1_label1.tlabelcontrol_setcaption("2");
>     else if (i == )   < -------
>        $t.tform1_label1.tlabelcontrol_setcaption("3");
>    // ....
> };
>
> Michael Dreher
>

Could this be because the type you do the selection from is an Integer,
and not the enumeration type?
Fri, Aug 14 2015 2:16 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< I noticed the JavaScript output I send you is incomplete. >>

Yeah, the issue is this:

If you don't declare a variable of type TTestInstructionDataType, then the compiler treats the enumerated type as "not being referenced", and doesn't generate emitted identifiers for the enumerated type's members.  I've modified the compiler to check the members for references also, and that will fix the issue.

For now, just make sure to declare your I variable as being of type TTestInstructionDataType instead of Integer.

Tim Young
Elevate Software
www.elevatesoft.com
Image