You can type cast a variable, for example:
Code: Select all
someMethod(obj : Object)
vars
aCustomer : Customer;
begin
aCustomer := obj.Customer;
If the object is a customer, the code above will work, if not then an exception is thrown at runtime as Murray has pointed out.
You can check whether the object is a customer first e.g.
Code: Select all
someMethod(obj : Object)
vars
aCustomer : Customer;
begin
if obj.isKindOf( Customer ) then
aCustomer := obj.Customer;
else
// We do not have a customer.
endif;
If you're type casting, then you should consider whether you are better to refactor the code and moving the logic onto a method on the Customer class etc.
However there are some times where it's unavoidable. For example, the display row method on the Table class has the following signature
Code: Select all
displayRow(
table: Table input;
theSheet: Integer;
obj: Object;
theRow: Integer;
bcontinue: Boolean io): String;
Most people will type case the obj variable in the code. In this case you can however update the signature to match the collection being used, e.g.
Code: Select all
displayRow(
table: Table input;
theSheet: Integer;
customerToDisplay: Customer;
theRow: Integer;
bcontinue: Boolean io): String;
which then avoids the type casting.