Var (keyword)

From RemObjects Software

Jump to: navigation, search

This is a Oxygene Keyword topic
Feel free to add your notes to this topic below.


var is used to define and optionally initialize a variable. In classic Pascal and Delphi, var was actually a separate section in the beginning of a method. While this still works in Oxygene, you can use var inline with your code.

Old style var section

method DoSomething;
var
  someInt: Integer;
  someBoolean: Boolean;
begin
  //some code
end;

var as inline statement

method DoSomething;
begin
  var someInt: Integer;
  //some code
  var someBoolean: Boolean;
  //some more code
  var AnswerToLifeTheUniverseAndEverything: Integer := 42;
end;

the var keyword can also be used in combination with Type Inference

method DoSomething;
begin
  var someInt := SomeMethodThatReturnsAnInteger();
  //some code
  var someBoolean := SomeMethodThatReturnsABoolean();
  //some more code
end;

In the above code the two variables are still strongly typed, but there type is inferred from the method call return type.

Also var can be used to declare method parameters:

method Test1(a1: Word; var a2: Integer; var a4: Byte);

Changing value of this parameters will affect the caller.


See Also


Product: RemObjects Oxygene (formerly known as Chrome)
Current version: 3.0 Previous Versions: 'Joyride' (2.0), 'Floorshow' (1.5), 'Adrenochrome' (1.0)

GlossaryKeywordsLanguage FeaturesPlatform FeaturesSamplesArticlesHow ToIssues

Personal tools