Enums vs Const vs Class Const in Delphi programming

后端 未结 6 1920
长情又很酷
长情又很酷 2021-02-19 06:31

I have an integer field in a ClientDataSet and I need to compare to some values, something like this:

I can use const

const
  mvValue1 = 1;
  mvValue2 =          


        
6条回答
  •  忘了有多久
    2021-02-19 07:05

    Declaration:

    type
      TMyValues = class
        type TMyEnum = (myValue1, myValue2, myValue3, myValue4);
        const MyStrVals: array [TMyEnum] of string =
          ('One', 'Two', 'Three', 'Four');
        const MyIntVals: array [TMyEnum] of integer =
          (1, 2, 3, 4);
      end;
    

    Usage:

    if ClientDataSet_Field.AsInteger = TMyValues.MyIntVals[myValue1] then
    

    A cast would generally be my last choice.

提交回复
热议问题