Wednesday 5 May 2010

Learning X++

Hi there

I've got an interview lined up next week that will require an X++ test. Now I've never even compiled an X++ program (except when you setup a test system but that's not really development) so I've been trying to learn using code snippets, blogs and MSDN

This is because I don't have access to a working virtual machine or test system since I'm not currently working with a Ax customer or partner. As far as I know, there aren't any resources available for solo students to study X++ on their own without paying for an expensive training course

Alas like my old Java teacher used to say "Programming is not a spectator sport". There's only so much that can be learned without having access to a virtual machine to play with or some kind of development system. Still this is better than nothing

So I've been going through the posts on the Issues Concerning X++ blog starting with the earliest post and this is what the past two hours of study have revealed:
You can find the original uncommented code sample and post here



static void Job2(Args _args)
{
System.Type typeOfByte;
System.Array arrayOfByte;
int arrayOfByteLength = 8;
int i;
;

typeOfByte = System.Type::GetType("System.Byte");
// Calls the static method GetType which is part of the
// System.Type class using the parameter System.Byte to
// instantiate the typeOfByte object to be a byte variable

arrayOfByte = System.Array::CreateInstance( typeOfByte, arrayOfByteLength );
// Initialising an array of bytes
// with 8 cells

for( i = 0; i < arrayOfByteLength; ++i )
{
arrayOfByte.SetValue( System.Convert::ToByte(i), i );
// SetValue takes Object - need to cast explicitely
}
// Go through the array and set the value in each
// cell to be the same as it's index number
// BUT cast the value from the initial integer
// value to a byte object

for( i = 0; i < arrayOfByteLength; ++i )
{
print ClrInterop::getAnyTypeForObject( arrayOfByte.GetValue( i ) );
// Displays the contents of the arrayOfByte using
// a Common Language Runtime static method that
// converts a CLR object to the X++ object type
// anytype. The anytype object's main use is as
// a return type so this is exactly the kind of
// situation it should be used
}

pause;
// Halts the execution of this job, presumably
// so that you can read the results of the
// above print statement
}


If you spot any mistakes then please feel free to point them out, like I said before, I'm still learning and in a odd kind of way

P.S. Writing and formatting this post so that it looks right took longer than the study time of the code snippet!

2 comments:

Anonymous said...

There is no need to use CLR calls for what you are doing (that is unless you are experimenting with CLR Interop). Arrays & integers are supported natively within X++.

However, this does compile and run. It could just be done in a cleaner manner and w/o the CLR.

Ajay Odedra said...

Firstly, could you at least sign some kind of name to your comments even if it's just a made up one so we can tell you apart from the other anonymous commentators

It seems the original post wasn't clear enough,(this has been modified now) I'm not actually running or compiling this code, this is purely a learning exercise

I don't have access to a working Ax system or virtual machine since I'm not working with a partner or customer at the moment

So one way I've found to study X++ in this situation is to pick apart code snippets on the internet, this is an example of that