Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: using a type with a case


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Feb 26, 2017 9:55 AM Last Post By: Cristian Peța
Frank Yingst

Posts: 175
Registered: 1/19/05
using a type with a case  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 24, 2017 9:09 PM
type
StationName = ( Select, Shell, SlimO_Main, SlimO_WX);
// The way I understand it the above should contain values 0,1,2,3
// but when I attempt the below code it gets rejected by the compiler
// E2010 Incompatible types 'Integer' and 'StationName'
// What do I need to do differently? Perhaps I need to use some sort of cast like in C?
// Should I have bought C++Builder instead?

case cmbStation.ItemIndex of
StationName.Shell : typeletters('SS');
StationName.SlimO_Main : typeletters('C');
StationName.SlimO_WX : typeletters('C');
end;

I also tried it like this but still got the incompatible types error:

case cmbStation.ItemIndex as StationName of
Shell : typeletters('SS'); // Incompatible types: 'Integer' and 'StationName'
SlimO_Main : typeletters('C');
SlimO_WX : typeletters('C');
end;

Of course I can do it like the below, but I don't like the looks of it:

case cmbStation.ItemIndex of
1 : typeletters('SS');
2 : typeletters('C');
3 : typeletters('C');
end;

I figured it out. Did it like this:
case cmbStation.ItemIndex of
ord(StationName.Shell) : typeletters('SS');
ord(StationName.SlimO_Main) : typeletters('C');
ord(StationName.SlimO_WX) : typeletters('C');
end;

Problem solved.
Frank Yingst

Posts: 175
Registered: 1/19/05
Re: using a type with a case  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 24, 2017 11:22 PM   in response to: Frank Yingst in response to: Frank Yingst
n/a
Cristian Peța

Posts: 157
Registered: 8/7/06
Re: using a type with a case  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2017 12:16 PM   in response to: Frank Yingst in response to: Frank Yingst
Why not:

case StationName(cmbStation.ItemIndex) of
  StationName.Shell : typeletters('SS');
  StationName.SlimO_Main : typeletters('C');
  StationName.SlimO_WX : typeletters('C');
end;


Edited by: Cristian Peta on Feb 25, 2017 10:16 PM
Roy Lambert

Posts: 1,063
Registered: 8/7/01
Re: using a type with a case [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2017 11:51 PM   in response to: Cristian Peța in response to: Cristian Peța
Cristian

I could be totally misunderstanding what you're trying to do but I think what you want is

case StationName of
  Shell : typeletters('SS');
  SlimO_Main : typeletters('C');
  SlimO_WX : typeletters('C');
end;


Here's a real life example

function GetFieldType(Fld: TField): string;
begin
case Fld.DataType of
ftInteger: Result := 'INTEGER';
ftString: Result := 'VARCHAR(' + IntToStr(Fld.Size) + ')' + VarCharCollation(LowerCase(Fld.FieldName));
ftFixedChar: Result := 'CHAR(' + IntToStr(Fld.Size) + ')';
ftBoolean: Result := 'BOOLEAN';
ftDate: Result := 'DATE';
ftFloat, ftCurrency: Result := 'FLOAT';
ftTime: Result := 'TIME';
ftMemo: Result := 'CLOB COLLATE "ANSI"';
ftBlob: Result := 'BLOB COLLATE "ANSI"';
ftGUID: Result := 'GUID COLLATE "ANSI"';
ftDateTime: Result := 'TIMESTAMP';
ftSmallInt: Result := 'SMALLINT';
ftAutoInc: Result := 'INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1) NOT NULL';
ftLargeInt: Result := 'BIGINT';
// ftCurrency: Result := 'DECIMAL(0, 2)';
ftBCD: Result := 'DECIMAL(0, 4)';
ftBytes: Result := 'BYTE(' + IntToStr(Fld.Size) + ')';
else Result := 'VARCHAR(' + IntToStr(Fld.Size) + ')' + VarCharCollation(LowerCase(Fld.FieldName));
end;
end;

Fld,Datatype is declared as property DataType: TFieldType read FDataType;

and TFieldType is

TFieldType = (ftUnknown, ftString, ftSmallint, ftInteger, ftWord, // 0..4
ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime, // 5..11
ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo, // 12..18
ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, ftFixedChar, ftWideString, // 19..24
ftLargeint, ftADT, ftArray, ftReference, ftDataSet, ftOraBlob, ftOraClob, // 25..31
ftVariant, ftInterface, ftIDispatch, ftGuid, ftTimeStamp, ftFMTBcd, // 32..37
ftFixedWideChar, ftWideMemo, ftOraTimeStamp, ftOraInterval); // 38..41

Roy Lambert
Cristian Peța

Posts: 157
Registered: 8/7/06
Re: using a type with a case [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2017 9:55 AM   in response to: Roy Lambert in response to: Roy Lambert
Roy Lambert wrote:
Cristian

I could be totally misunderstanding what you're trying to do but I think what you want is

case StationName of
  Shell : typeletters('SS');
  SlimO_Main : typeletters('C');
  SlimO_WX : typeletters('C');
end;

StationName is a type...
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02