What is the .toString() varient in X++ or Dynamics AX?
Asked Answered
P

4

9

Is there something similar to the famous toString() method of C# in Axapta?

I try to run underlying code:

info(this.dataSource());

But it gives me this error message: "Argument 'txt' is incompatible with the required type."

Prepared answered 7/3, 2012 at 13:22 Comment(1)
info expects a string value. There is no automatic conversion.Clausewitz
C
9

The toString is available on all objects but usually not of much value:

info(this.dataSource().toString())

This gives this output:

Class FormDataSource Address

Probably you knew that already! However the query datasource does give something useful:

FormDataSource fds = this.dataSource();
;
info(fds.query().dataSourceTable(tableNum(Address)).toString());

gives the corresponding SQL query:

SELECT FIRSTFAST * FROM Address
Clausewitz answered 7/3, 2012 at 15:52 Comment(2)
.toString() is useful on AX collection classes as well, such as the Array type. Array.toString() will often output readable contents. If you have an array of classes or something odd, perhaps not.Jeffiejeffrey
I always try to avoid using datSourceNo() in production code because the dataSourceNo can change. If you modify the form or report and add or remove (or even change the order of) dataSources, this will break your code. I have seen this occur in base Microsoft code when upgrading from 4.0 to 2009, they changed the order of some datasources and it broke lookups that were using the DataSourceNo. I always try to use dataSource().Name(), becuase that is much less likely to change.Penitential
P
2

If you are only looking for the name of the dataSource you can do the following:

info(this.dataSource().name());
Penitential answered 8/3, 2012 at 13:36 Comment(0)
P
1

Unfortunately not, but there are a number of "...2Str()" methods for converting base data types to string, for example;

int2Str() http://technet.microsoft.com/en-us/library/aa851371(v=ax.50).aspx

int642str() http://technet.microsoft.com/en-us/library/aa851371(v=ax.50).aspx

date2str() http://msdn.microsoft.com/en-us/library/aa857241(v=ax.10).aspx

Plus others.

Parasynthesis answered 7/3, 2012 at 13:32 Comment(1)
@IllDevelopIt I will update answer - are you trying to get the datasource description or the contents to a string!?!Parasynthesis
H
1

I just want to add that I often use strFmt.

Counter c = 25;
int id = 3;
;
info(strfmt("Record number %1, id = %2", c, a)); //Record number 25, id = 3 

It is similar to String.Format() in C#. You can see more details here.

Hilliary answered 5/5, 2016 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.