Friday, April 3, 2015

Anytype Data Type

What is Anytype Data Type

Anytype is a placeholder for any data types (integer, real, string, container, class, etc.). The data type will be initialized to the anytype variable when you assign a value. For example:

anytype test; //Declare anytype variable
test = 10; //assign a integer value

Variable initialize

After assigned the integer value we can treat anytype variable as integer variable.
Once the data type initialized to the anytype variable, we can’t assign other data type values. For example:

anytype test; //Declare anytype variable 
test = 10; //assign a integer value 
test = 'strValue'; 
info(strFmt('%1', test)); 

The info window will show the message “0”

Avoid "Wrong argument type for function" Error

One more thing, you will get a runtime error message when you use the anytype variable before you assign the value. But, we won’t get any compilation error. For example:

anytype test; //Declare anytype variable 
info(strFmt('%1', test));

Error message:
Error executing code: Wrong argument type for function.

So, before use the anytype variable, we can check the anytype variable is initialized or not. For Example:

anytype test; //Declare anytype variable 
if(!(typeOf(test) == Types::AnyType)) 

    info(strFmt('%1', test)); 
}

If we want to know the which datatype was set in the anytype variable, we can use the typeOf() method.

Using Anytype in Map class

We can use Anytype in the map class. For example:

Map TestMap = new Map(Types::Int64, Types::String); 
TestMap.insert(1, 'Test'); 
TestMap.insert(2, 1); 
TestMap.insert(3, 3.5); 
info(strFmt('%1', TestMap.lookup(3)));

No comments :

Post a Comment