Learn how to mapping Enumerations with TWinControl in Delphi

Enumerations
The provision of enumerations is a big plus for Delphi. They make for readable and reliable code. An enumeration is simply a fixed range of named values. For example, the Boolean data type is itself an enumeration, with two possible values : True and False. If you try to assign a different value to a boolean variable, the code will not compile.

How to defining enumerations

Ex.
TCalculatorKind = (calAdd, calSubtract);


The TCalculatorKind type definition creates a new Delphi data type that we can use as a type for any new variable in our prgram. (If you define types that you will use many times, you can place them in a Unit file and refer to this in a uses statement in any program that wants to use them). We have defined an enumeration range of names that represent the suits of playing cards.


In fact, each of the enumeration values is equated with a number. The TCalculatorKind enumeration will have the following values assigned

calAdd = 0, calSubtract = 1


How to mapping Enumerations with TWinControl in Delphi.

type
  TCalculatorKind = (calAdd, calSubtract);

var
 calKind: TCalculatorKind = TCalculatorKind(0);


Test with Combobox1/ Add item to combobox.

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items.Clear;
  ComboBox1.Items.Add('Add');
  ComboBox1.Items.Add('Subtract');
  ComboBox1.ItemIndex := 0;
end;


How to detect Enumerates value.
procedure TForm1.BtnDetectCalKindClick(Sender: TObject);
begin
    case calKind of
      calAdd: ShowMessage('Calculator kind is: Add.');
      calSubtract: ShowMessage('Calculator kind is: Subtract.');
  end;
end;

Source





Comments