Good morning,
I am trying to declare constant TPoint in Delphi, how do you do that ?
None of the following works
// ...
const POS_A : Tpoint = (3,3);
const POS_B : Tpoint = Point(3,10);
const POS_C : Tpoint = Tpoint(3,20);
const POS_D : Tpoint = TPoint.create(3,30);
// ...
Any clue ?
According to the documentation on constants, specifically the part about records, you write
const
MyPoint: TPoint = (X: 3; Y: 7);
Related
In Dart Object() constructor is declared as const so:
identical(const Object(), const Object()); //true
I know that in Dart 2 the keyword const is optional and I thought that the previous statement was equivalent to:
identical(Object(), Object()); //false
But actually it seems to be equivalent to:
identical(new Object(), new Object()); //false
Now my doubts are:
1) When is const keyword optional?
2) Is there any way to ensure instances of my classes to be always constant without const keyword? So that I can obtain:
indentical(MyClass(), MyClass()); //true (is it possible?)
Dart 2 allows you to omit new everywhere. Anywhere you used to write new, you can now omit it.
Dart 2 also allows you to omit const in positions where it's implied by the context. Those positions are:
Inside a const object creations, map or list literal (const [1, [2, 3]]).
Inside a const object creation in metadata (#Foo(Bar()))
In the initializer expression of a const variable (const x = [1];).
In a switch case expression (case Foo(2):...).
There are two other locations where the language requires constant expressions, but which are not automatically made constant (for various reasons):
Optional parameter default values
initializer expressions of final fields in classes with const constructors
1 is not made const because we want to keep the option of making those expressions not need to be const in the future. 2 is because it's a non-local constraint - there is nothing around the expression that signifies that it must be const, so it's too easy to, e.g., remove the const from the constructor without noticing that it changes the behavior of the field initializer.
const is optional in a const context. Basically a const context is created when the expression has to be const to avoid compilation error.
In the following snippet you can see some place where const is optional:
class A {
const A(o);
}
main(){
// parameters of const constructors have to be const
var a = const A(const A());
var a = const A(A());
// using const to create local variable
const b = const A();
const b = A();
// elements of const lists have to be const
var c = const [const A()];
var c = const [A()];
// elements of const maps have to be const
var d = const {'a': const A()};
var d = const {'a': A()};
}
// metadatas are const
#A(const A())
#A(A())
class B {}
You can find more details in Optional new/const and Implicit Creation.
I am trying to work with an array of colors that is a constant. But Delphi keeps giving me compiling errors. I can't seem to figure out what I'm doing wrong...
const
Statuses : array[0..3] of TAlphaColors =
(
TAlphaColors.Lightgray, //error here: says insert a '(', even though I already have one
TAlphaColors.Yellow,
TAlphaColors.Limegreen,
TAlphaColors.Blue
);
The problem you face is that TAlphaColor.Lightgray, and indeed all the other TAlphaColor.XXX that you name are ordinal true constant. Whereas TAlphaColors is a record type.
Let's take a look at the relevant definitions:
type
TAlphaColor = type Cardinal;
TAlphaColorRec = record
const
Alpha = TAlphaColor($FF000000);
Aliceblue = Alpha or TAlphaColor($F0F8FF);
Antiquewhite = Alpha or TAlphaColor($FAEBD7);
.... many more color constant omitted
constructor Create(const Color: TAlphaColor);
class var ColorToRGB: function (Color: TAlphaColor): Longint;
case LongWord of
0:
(Color: TAlphaColor);
2:
(HiWord, LoWord: Word);
3:
{$IFDEF BIGENDIAN}
(A, R, G, B: System.Byte);
{$ELSE}
(B, G, R, A: System.Byte);
{$ENDIF}
end;
So the constants are not of type TAlphaColorRec. Indeed it's one of the great frustrations of the language that you cannot declare nested constants in a record that are of that record type. These constants are ordinal true constants.
Note that the record itself has data in a variant part of the record. And the field of interest is the Color field. So, you could declare your constant array like so:
const
Statuses : array[0..3] of TAlphaColors = (
(Color: TAlphaColors.Lightgray),
(Color: TAlphaColors.Yellow),
(Color: TAlphaColors.Limegreen),
(Color: TAlphaColors.Blue)
);
If only Embarcadero had had the foresight to allow us to write code like this:
type
TMyRecord = record
public
const
MyConst: TMyRecord = ();
end;
I'm modifying some existing code which uses OpenPrinter to instead use OpenPrinter2. It contains one more parameter PPRINTER_OPTIONS which in Delphi is encapsulated as a TPrinterOptions, defined like so:
_PRINTER_OPTIONSW = record
cbSize: Cardinal;
dwFlags: DWORD;
end;
I'm having trouble understanding how to use this struct to encapsulate the flag PRINTER_OPTION_NO_CACHE, and I can't even find that constant in any of the existing VCL/RTL.
var
PD: TPrinterDefaults;
PO: TPrinterOptions;
begin
PO.dwFlags:= ???
if OpenPrinter2(ADevice, #FPrinterHandle, #PD, #PO) then begin
...
I'm not having much luck searching for Delphi implementation of either OpenPrinter2 or TPrinterOptions. How do I make PRINTER_OPTION_NO_CACHE work?
EDIT
Is this correct?
const
PRINTER_OPTION_NO_CACHE = 0;
PRINTER_OPTION_CACHE = 1;
PRINTER_OPTION_CLIENT_CHANGE = 2;
...
PO.dwFlags:= PRINTER_OPTION_NO_CACHE;
The MSDN docs say this:
typedef enum tagPRINTER_OPTION_FLAGS {
PRINTER_OPTION_NO_CACHE,
PRINTER_OPTION_CACHE,
PRINTER_OPTION_CLIENT_CHANGE
} PRINTER_OPTION_FLAGS;
A C enum, as declared here, is really just an int. The first enum value is 0, the next 1, and so on.
But the header file tells a different story, contradicting the documentation. The header file says:
typedef enum _PRINTER_OPTION_FLAGS
{
PRINTER_OPTION_NO_CACHE = 1 << 0,
PRINTER_OPTION_CACHE = 1 << 1,
PRINTER_OPTION_CLIENT_CHANGE = 1 << 2,
PRINTER_OPTION_NO_CLIENT_DATA = 1 << 3,
} PRINTER_OPTION_FLAGS;
So translate to Pascal like this.
const
PRINTER_OPTION_NO_CACHE = 1;
PRINTER_OPTION_CACHE = 2;
PRINTER_OPTION_CLIENT_CHANGE = 4;
PRINTER_OPTION_NO_CLIENT_DATA = 8;
Populate the record like this:
var
Options: TPrinterOptions;
....
Options.cbSize := SizeOf(Options);
Options.dwFlags := PRINTER_OPTION_NO_CACHE;
I am trying to define a const in Delphi (Delphi 2005 that is) that is based on other constants, but Delphi complains about it not being a constant expression. This is what my code looks like:
program myProgram;
const
Xpoints = 20;
Ypoints = 30;
ArraySize = trunc(sqrt(Xpoints*Ypoints));
var
myArray : array[1..ArraySize] of integer;
I could do ArraySize = Xpoints*Ypoints but the sqrt causes problems. The idea is that I would like the array to be sized by the constants Xpoints and Ypoints. I could do something like this:
program myProgram;
const
sqrtXpoints = 4.472135956;
sqrtYpoints = 5.47722557506;
Xpoints = trunc(sqrtXpoints*sqrtXpoints);
Ypoints = trunc(sqrtYpoints*sqrtYpoints);
ArraySize = trunc(sqrtXpoints*sqrtYpoints);
var
myArray : array[1..ArraySize] of integer;
taking care to slightly overestimate the square root values for the trunc. Everything will update correctly if I change sqrtXpoints or sqrtYpoints, but this approach just seems so... stupid.
As a temporary fix I can evaluate the constant myself like this:
program myProgram;
const
Xpoints = 20;
Ypoints = 30;
ArraySize = 24;
var
myArray : array[1..ArraySize] of integer;
but I don't like this because ArraySize does not automatically update if I change Xpoints or Ypoints.
It seems like the compiler should know how to evaluate a constant defined as a mathematical function of another constant at compile time for things like the example above and for even simpler things like this:
const
pi = 4.0*arctan(1.0);
but I can't get it to accept it. Any suggestions? Thanks in advance for your help!
Delphi do not allow to use most of functions in definition of constants.
But you can easy solve it, just use dynamic arrays and you will be able to calculate all what you want from your code:
const
Xpoints = 20;
Ypoints = 30;
var
myArray : array of integer;
procedure TForm9.FormCreate(Sender: TObject);
begin
setlength(myArray, trunc(sqrt(Xpoints*Ypoints)));
end;
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 = 2;
if ClientDataSet_Field.AsInteger = mvValue1 then
or enums
TMyValues = (mvValue1 = 1, mvValue2 = 2);
if ClientDataSet_Field.AsInteger = Integer(mvValue1) then
or class const
TMyValue = class
const
Value1 = 1;
Value2 = 2;
end;
if ClientDataSet_Field.AsInteger = TMyValues.Value1 then
I like the class const approach but it seems that is not the delphi way, So I want to know what do you think
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.
I wouldn't say that class consts are not the Delphi way. It's just they have been introduced to Delphi quite recently, and a lot of books and articles you'll find on the internet were written before their introduction, and thus you won't see them widely used. Many Delphi developers (I'd say the majority) will have started using Delphi before they were made available, and thus they're not the first thing that one thinks about.
One thing to consider is backwards compatibility - class constants are relatively new to Delphi so if your code has to be sharable with previous versions than they are out.
I typically use enumerated types, with the difference from yours is that my first enumeration is usually an 'undefined' item to represent NULL or 0 in an int field.
TmyValues = (myvUndefined, myvDescription1, myvDescription2)
if ClientDataSet_Field.AsInteger = Ord(myvDescription1) then...
To use a little bit of Jim McKeeth's answer - if you need to display to the user a text viewable version, or if you need to convert their selected text into the enumerated type, then an array comes in handy in conjuction with the type:
const MYVALS: array [TmyValues ] of string = ('', 'Description1', 'Description2');
You can then have utility functions to set/get the enumerated type to/from a string:
Function MyValString(const pMyVal:TmyValues):string;
begin
result := MYVALS[Ord(pMyVal)];
end;
Function StringToMyVal(const pMyVal:String):TMyValues;
var i:Integer;
begin
result := myvUndefined;
for i := Low(MYVALS) to High(MYVALS) do
begin
if SameText(pMyVal, MYVALS[i]) then
begin
result := TMyValues(i);
break;
end;
end;
end;
Continuing on... you can have scatter routine to set a combo/list box:
Procedure SetList(const DestList:TStrings);
begin
DestList.Clear;
for i := Low(MYVALS) to High(MYVALS) do
begin
DestList.Insert(MYVALS[i]);
end;
end;
In code: SetList(Combo1.Items) or SetList(ListBox1.Items)..
Then if you are seeing the pattern here... useful utility functions surrounding your enumeration, then you add everything to it's own class and put this class into it's own unit named MyValueEnumeration or whaterver. You end up with all the code surrounding this enumeration in one place and keep adding the utility functions as you need them. If you keep the unit clean - don't mix in other unrelated functionality then it will stay very handy for all projects related to that enumeration.
You'll see more patterns as time goes and you use the same functionality over and over again and you'll build a better mousetrap again.
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;
so many options! :-) i prefer enums and routinely use them as you describe. one of the parts i like is that i can use them with a "for" loop. i do use class constants as well but prefer enums (even private enums) depending on what i'm trying to achieve.
TMyType=class
private const // d2007 & later i think
iMaxItems=1; // d2007 & later i think
private type // d2007 & later i think
TMyValues = (mvValue1 = 1, mvValue2 = 2); // d2007 & later i think
private
public
end;
An option you haven't thought of is to use a lookup table in the database and then you can check against the string in the database.
eg.
Select value, Description from tbl_values inner join tbl_lookup_values where tbl_values.Value = tbl_lookup_values.value
if ClientDataSet_Field.AsString = 'ValueIwant' then