Enums vs Const vs Class Const in Delphi programming

后端 未结 6 1898
长情又很酷
长情又很酷 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 06:52

    When using constants I recommend assigning the type when the data type is a numeric float.

    Delphi and other languages will not always evaluate values correctly if the types do not match...

    TMyValue = class
    const
       // will not compare correctly to float values.
       Value1 = 1; // true constant can be used to supply any data type value
       Value2 = 2; // but should only be compared to similar data type
    
       // will not compare correctly to a single or double.
       Value3 = 3.3; // default is extended in debugger 
    
       // will not compare correctly to a single or extended.
       Value1d : double = Value1; // 1.0
       Value2d : double = Value2; // 2.0
    end;
    

    Compared float values in if () and while () statements should be compared to values of the same data type, so it is best to define a temporary or global variable of the float type used for any comparison statements (=<>).

    When compared to the same float data type this format is more reliable for comparison operators in any programming language, not just in Delphi, but in any programming language where the defined float types vary from variable to constant.

    Once you assign a type, Delphi will not allow you to use the variable to feed another constant, so true constants are good to feed any related data type, but not for comparison in loops and if statements, unless they are assigned and compared to integer values.

    ***Note: Casting a value from one float type to another may alter the stored value from what you entered for comparison purposes, so verify with a unit test that loops when doing this.

    It is unfortunate that Delphi doesn't allow an enumeration format like... TController : Integer = (NoController = 0, ncpod = 1, nextwave = 2);

    or enforce the type name for access to the enumeration values.

    or allow a class constant to be used as a parameter default in a call like... function getControllerName( Controller : TController = TController.NoController) : string;

    However, a more guarded approach that provides both types of access would be to place the enumeration inside a class.

     TController = class
      //const
         //NoController : Integer = 1;
         //ncpod : Integer = 2;
         //nextwave : Integer = 3;
    
      type
         Option = (NoController = 0, ncpod = 1, nextwave = 2);
    
      public
        Class function Name( Controller : Option = NoController) : string; static;
     end;
    
     implementation
    
     class function TController.Name( Controller : Option = NoController) : string;
     begin
        Result := 'CNC';
        if (Controller = Option.nextwave) then
           Result := Result + ' Piranha'
        else if (Controller = Option.ncpod) then
           Result := Result + ' Shark';
    
        Result := Result + ' Control Panel';
     end;
    

    This approach will effectively isolate the values, provide the static approach and allow access to the values using a for () loop.

    The access to the values from a floating function would be like this...

     using TControllerUnit;
    
     function getName( Controller : TController.Option = TController.Option.NoController) : string;
    
     implementation
    
     function getName( Controller : TController.Option = TController.Option.NoController) : string;
     begin
       Result := 'CNC';
       if (Controller = TController.Option.nextwave) then
           Result := Result + ' Piranha'
       else if (Controller = TController.Option.ncpod) then
           Result := Result + ' Shark';
    
       Result := Result + ' Control Panel';
     end;
    

提交回复
热议问题