Greg On Dynamics Ax

Random thoughts about development using Dynamics AX and .Net

One thing you should know about the x++ Type ‘anytype’

with one comment

I came across this little gem the other day.

If you declare a variable of type ‘anytype’, assign its value to an enumerated type, then try and reassign it to a date, the code will compile but you will get an error at runtime.

Here is an example

static void AnytypeAssignmentError(Args _args)
{
    anytype enumOrDate;
    ;
    enumOrDate = NoYes::Yes;
    enumOrDate = 06\2\2009; // this should be the date 6th Feb 2009 in x++
}

And here is the error when you run it:
assignmenterror

So it works in a very similar way to the C# 3.0 type ‘var’, with the exception that you will not get a compile error in x++.

The documentation explains anytype should not be used in this way:

If anytype is used as a variable, you must assign a value to it before it can be used or you will get a run-time error. After you have assigned a value to it, you cannot convert it to another data type.

See here for more detail MSDN anytype reference page

Written by gregondax

February 6, 2009 at 1:41 pm

One Response

Subscribe to comments with RSS.

  1. Greg,

    I think you may have misunderstood the var feature in C#. It is used only to allow the compiler to infer the type (which is always known at compile time) from the usage. This is not just a convenience, it is vital when dealing with anonymous types.

    The comparison is better if you use dynamic types in C#, or refer to untyped variables, as provided by some scripting languages.

    pvillads

    October 12, 2009 at 8:27 pm


Leave a reply to pvillads Cancel reply