Delphi |
|
To convert just use the new type name as function and apply it to the variable:
var
C: Char;
B: Byte;
...
C:='G';
B:=Byte(C);
In Delphi you can convert only size-compatible types, so you cannot convert, for instance, Double to Integer.
If you convert the class types, you can use as and is operators. is operator checks the class type and return True, if the class of is needed type or derived from the needed type. as makes the same chacking as is operator, then converts type. If the type in as expression is not compatible, exception is generated:
var
SomeComponent: TComponent;
...
SomeComponent:=TButton.Create(Self);
...
if SomeComponent is TButton then
TButton(SomeComponent).ModalResult:=mrNone;
if SomeComponent is TControl then
TControl(SomeComponent).Width:=77;
if SomeComponent is TMemo then
// the following code will be not executed
TMemo(SomeComponent).Lines.Text:='Defaul memo text';
(SomeComponent as TButton).ModalResult:=mrOk;
// the following code will be not executed and exception will raise
(SomeComponent as TMemo).WordWrap:=False;
|