<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>SysDictCoder</title>
	
	<link>http://sysdictcoder.com/blog</link>
	<description>Dynamics Ax stuff</description>
	<lastBuildDate>Fri, 30 Apr 2010 17:07:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/sysdictcoder" /><feedburner:info uri="sysdictcoder" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /><item>
		<title>Copy records</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/DbgHIyCgqGQ/</link>
		<comments>http://sysdictcoder.com/blog/copy-records-142/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 17:06:21 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Dynamics Ax (Axapta)]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/?p=142</guid>
		<description><![CDATA[A while ago I wrote a simple function to compare records.  If you want to do something else check out Patrik Luca&#8217;s copy function.  Another example how to use reflection.
]]></description>
			<content:encoded><![CDATA[<p>A while ago I wrote a simple function to <a href="http://sysdictcoder.com/blog/comparing-records/">compare records</a>.  If you want to do something else check out Patrik Luca&#8217;s <a href="http://patrikluca.blogspot.com/2010/04/x-code-snippet-copying-records.html">copy function</a>.  Another example how to use reflection.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/DbgHIyCgqGQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/copy-records-142/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/copy-records-142/</feedburner:origLink></item>
		<item>
		<title>Comparing records</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/3IW37fNgwlQ/</link>
		<comments>http://sysdictcoder.com/blog/comparing-records/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 09:35:01 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Dynamics Ax (Axapta)]]></category>
		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/?p=55</guid>
		<description><![CDATA[Sometimes you need to know what&#8217;s the difference between records.  I made a simple function to do just that.  It&#8217;s an example of how to use reflection and field IDs on records.
It takes two records and returns a container with the field IDs and the values from both records.  For simplicity I [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to know what&#8217;s the difference between records.  I made a simple function to do just that.  It&#8217;s an example of how to use <a href="http://sysdictcoder.com/blog/reflection-with-the-dictionary/">reflection</a> and <a href="http://sysdictcoder.com/blog/tricks-with-xpp-embedded-sql/">field IDs on records</a>.</p>
<p>It takes two records and returns a container with the field IDs and the values from both records.  For simplicity I used a flattened container instead of more complicated data structures.  Feel free to replace it with nested containers or some kind of collection.</p>
<p>I added this to the class Global for easy access.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">container</span> compareRecords<span style="color: #000000;">&#40;</span>Common _record1<span style="color: #00007f;">,</span> Common _record2<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    SysDictTable    dictTable <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> SysDictTable<span style="color: #000000;">&#40;</span>_record1.<span style="color: #000000;">TableId</span><span style="color: #000000;">&#41;</span>;
    SysDictField    dictField;
    FieldId         fieldId<span style="color: #00007f;">,</span> extFieldId;
    <span style="color: #0000ff;">container</span>       ret;
    <span style="color: #0000ff;">int</span>             i<span style="color: #00007f;">,</span> j;
    ;
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>_record1.<span style="color: #000000;">TableId</span> <span style="color: #00007f;">!=</span> _record2.<span style="color: #000000;">TableId</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">conNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i<span style="color: #00007f;">=</span><span style="color: #000000;">1</span>; i<span style="color: #00007f;">&lt;=</span>dictTable.<span style="color: #000000;">fieldCnt</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #00007f;">++</span>i<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        fieldId <span style="color: #00007f;">=</span> dictTable.<span style="color: #000000;">fieldCnt2Id</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>;
        dictField <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> SysDictField<span style="color: #000000;">&#40;</span>_record1.<span style="color: #000000;">tableId</span><span style="color: #00007f;">,</span> fieldId<span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #00007f;">!</span>dictField.<span style="color: #000000;">isSystem</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>j<span style="color: #00007f;">=</span><span style="color: #000000;">1</span>; j<span style="color: #00007f;">&lt;=</span> dictField.<span style="color: #000000;">arraySize</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #00007f;">++</span>j<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                extFieldId <span style="color: #00007f;">=</span> fieldId2Ext<span style="color: #000000;">&#40;</span>fieldId<span style="color: #00007f;">,</span> j<span style="color: #000000;">&#41;</span>;
&nbsp;
                <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>_record1.<span style="color: #000000;">&#40;</span>extFieldId<span style="color: #000000;">&#41;</span> <span style="color: #00007f;">!=</span> _record2.<span style="color: #000000;">&#40;</span>extFieldId<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    ret <span style="color: #00007f;">+=</span> <span style="color: #000000;">&#91;</span>extFieldId<span style="color: #00007f;">,</span> _record1.<span style="color: #000000;">&#40;</span>extFieldId<span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span> _record2.<span style="color: #000000;">&#40;</span>extFieldId<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>;
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> ret;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see it only compares records of the same type and skips system fields (e.g. RecId).  Special care is taken to handle array fields correctly.</p>
<p>Using it is quite straightforward.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> demoCompareRecords<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    CustTable   custTable1 <span style="color: #00007f;">=</span> CustTable<span style="color: #00007f;">::</span><span style="color: #000000;">find</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'1101'</span><span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// CEE demo data</span>
    CustTable   custTable2 <span style="color: #00007f;">=</span> CustTable<span style="color: #00007f;">::</span><span style="color: #000000;">find</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'1102'</span><span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// CEE demo data</span>
&nbsp;
    <span style="color: #0000ff;">container</span>   con;
    <span style="color: #0000ff;">int</span>         i;
    ;
&nbsp;
    con <span style="color: #00007f;">=</span> Global<span style="color: #00007f;">::</span><span style="color: #000000;">compareRecords</span><span style="color: #000000;">&#40;</span>custTable1<span style="color: #00007f;">,</span> custTable2<span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i<span style="color: #00007f;">=</span><span style="color: #000000;">1</span>; i<span style="color: #00007f;">&lt;=</span><span style="color: #0000ff;">conLen</span><span style="color: #000000;">&#40;</span>con<span style="color: #000000;">&#41;</span>; i<span style="color: #00007f;">+=</span><span style="color: #000000;">3</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        info<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">strFmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;%1: '%2' &lt;-&gt; '%3'&quot;</span>
                   <span style="color: #00007f;">,</span><span style="color: #0000ff;">fieldId2Name</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">tableNum</span><span style="color: #000000;">&#40;</span>CustTable<span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span> <span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con<span style="color: #00007f;">,</span> i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                   <span style="color: #00007f;">,</span><span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con<span style="color: #00007f;">,</span> i<span style="color: #00007f;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
                   <span style="color: #00007f;">,</span><span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con<span style="color: #00007f;">,</span> i<span style="color: #00007f;">+</span><span style="color: #000000;">2</span><span style="color: #000000;">&#41;</span>
                   <span style="color: #000000;">&#41;</span>
             <span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/3IW37fNgwlQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/comparing-records/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/comparing-records/</feedburner:origLink></item>
		<item>
		<title>Axapta timeline</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/4hzh1wj7Jgs/</link>
		<comments>http://sysdictcoder.com/blog/axapta-timeline/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 18:28:14 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/?p=130</guid>
		<description><![CDATA[Ever wondered how Dynamics Ax came to be?  Check out the timeline I found at DynamicsWorld.
]]></description>
			<content:encoded><![CDATA[<p>Ever wondered how Dynamics Ax came to be?  Check out the <a href="http://www.dynamicsworld.co.uk/Dynamics-AX-Timeline.php">timeline</a> I found at DynamicsWorld.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/4hzh1wj7Jgs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/axapta-timeline/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/axapta-timeline/</feedburner:origLink></item>
		<item>
		<title>Intrinsic function weirdness</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/CfDj_FIusrs/</link>
		<comments>http://sysdictcoder.com/blog/intrinsic-function-weirdness/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 08:21:15 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Bug]]></category>
		<category><![CDATA[Dynamics Ax (Axapta)]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/?p=127</guid>
		<description><![CDATA[Over at Dynamics Ax Daily I found a post about a compile error using tableNum() in a select statement.
As mentioned there, the following code doesn&#8217;t compile.

    select extCodeTable
        where extCodeTable.ExtCodeTableId == tableNum&#40;CompanyInfo&#41;
    join extCodeValueTable
        where [...]]]></description>
			<content:encoded><![CDATA[<p>Over at <a href="http://axdaily.blogspot.com/">Dynamics Ax Daily</a> I found a post about a <a href="http://axdaily.blogspot.com/2010/02/intrinsic-functions-in-queries.html">compile error</a> using tableNum() in a select statement.</p>
<p>As mentioned there, the following code doesn&#8217;t compile.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">    <span style="color: #0000ff;">select</span> extCodeTable
        <span style="color: #0000ff;">where</span> extCodeTable.<span style="color: #000000;">ExtCodeTableId</span> <span style="color: #00007f;">==</span> <span style="color: #0000ff;">tableNum</span><span style="color: #000000;">&#40;</span>CompanyInfo<span style="color: #000000;">&#41;</span>
    <span style="color: #0000ff;">join</span> extCodeValueTable
        <span style="color: #0000ff;">where</span> extCodeValueTable.<span style="color: #000000;">ExtCodeId</span> <span style="color: #00007f;">==</span> extCodeTable.<span style="color: #000000;">ExtCodeId</span>;</pre></div></div>

<p>The compiler chokes on the call to tableNum().  I was surprised to see this, as I could have sworn that I have used tableNum() in select statements before.  As it turns out it does compile in some cases.</p>
<p>It compiles without the join.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">    <span style="color: #0000ff;">select</span> extCodeTable
        <span style="color: #0000ff;">where</span> extCodeTable.<span style="color: #000000;">ExtCodeTableId</span> <span style="color: #00007f;">==</span> <span style="color: #0000ff;">tableNum</span><span style="color: #000000;">&#40;</span>CompanyInfo<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>It also works if you add another where clause after tableNum().</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">    <span style="color: #0000ff;">select</span> extCodeTable
        <span style="color: #0000ff;">where</span> extCodeTable.<span style="color: #000000;">ExtCodeTableId</span> <span style="color: #00007f;">==</span> <span style="color: #0000ff;">tableNum</span><span style="color: #000000;">&#40;</span>CompanyInfo<span style="color: #000000;">&#41;</span>
             <span style="color: #00007f;">&amp;&amp;</span> extCodeTable.<span style="color: #000000;">RecId</span> <span style="color: #00007f;">!=</span> <span style="color: #000000;">0</span>
    <span style="color: #0000ff;">join</span> extCodeValueTable
        <span style="color: #0000ff;">where</span> extCodeValueTable.<span style="color: #000000;">ExtCodeId</span> <span style="color: #00007f;">==</span> extCodeTable.<span style="color: #000000;">ExtCodeId</span>;</pre></div></div>

<p>And using a non-intrinsic function works too.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">    <span style="color: #0000ff;">select</span> extCodeTable
        <span style="color: #0000ff;">where</span> extCodeTable.<span style="color: #000000;">ExtCodeTableId</span> <span style="color: #00007f;">==</span> <span style="color: #0000ff;">str2int</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;1234&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0000ff;">join</span> extCodeValueTable
        <span style="color: #0000ff;">where</span> extCodeValueTable.<span style="color: #000000;">ExtCodeId</span> <span style="color: #00007f;">==</span> extCodeTable.<span style="color: #000000;">ExtCodeId</span>;</pre></div></div>

<p>This example doesn&#8217;t make much sense with regards to business logic but it does compile.</p>
<p>I&#8217;m guessing this is a bug in the compiler.  As far as I can tell it only fails when you use an intrinsic function before a join clause.  Until it&#8217;s fixed just use a variable or throw in another where clause to check for RecId != 0.  Since all records have a RecId it won&#8217;t affect the results you get back.  This happens in Ax 4.0 and 2009.</p>
<p>All intrinsic functions are listed on <a href="http://msdn.microsoft.com/en-us/library/dd252591.aspx">MSDN</a>.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/CfDj_FIusrs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/intrinsic-function-weirdness/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/intrinsic-function-weirdness/</feedburner:origLink></item>
		<item>
		<title>Tricks with X++ embedded SQL</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/lHdqUyXRv6I/</link>
		<comments>http://sysdictcoder.com/blog/tricks-with-xpp-embedded-sql/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 16:26:40 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Dynamics Ax (Axapta)]]></category>
		<category><![CDATA[Tips & tricks]]></category>
		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/?p=99</guid>
		<description><![CDATA[There are some tricks that you can do in X++ SQL.  They&#8217;re not really well known because they&#8217;re not suited for normal use and there are not many clues in the editor that they exist.  But they do come in handy every now and then.
Getting data without declaring a table variable
It&#8217;s possible to [...]]]></description>
			<content:encoded><![CDATA[<p>There are some tricks that you can do in X++ SQL.  They&#8217;re not really well known because they&#8217;re not suited for normal use and there are not many clues in the editor that they exist.  But they do come in handy every now and then.</p>
<h2>Getting data without declaring a table variable</h2>
<p>It&#8217;s possible to get data from the database without declaring any table variables.  It goes something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Job3<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #007f00;">// No variables!</span>
    ;
&nbsp;
    <span style="color: #0000ff;">print</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">select</span> CustTable<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">AccountNum</span>;
    <span style="color: #0000ff;">pause</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note the parentheses around the select statement.  This tells the compiler to create a temporary, anonymous buffer to hold the record.  The select statement uses the exact table name and is immediately followed by a field name to get a value from a field.  </p>
<p>If you use this there&#8217;s no Intellisense to help you look up the field name because the editor doesn&#8217;t know the type of the anonymous buffer.</p>
<p>This technique is often used in exist() methods on tables.</p>
<h2>Getting a value from a field if you only know the field ID</h2>
<p>Sometimes you don&#8217;t know the exact field name but you do know a field ID.  Even in cases like this it&#8217;s possible to write an X++ SQL statement without knowing any field names.</p>
<p>Take a look at this example.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Job4<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    CustTable   custTable;
    FieldId     fieldId;
    ;
&nbsp;
    fieldId <span style="color: #00007f;">=</span> <span style="color: #0000ff;">fieldNum</span><span style="color: #000000;">&#40;</span>CustTable<span style="color: #00007f;">,</span> AccountNum<span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// This could be passed as a method parameter</span>
&nbsp;
    <span style="color: #0000ff;">select</span> custTable; <span style="color: #007f00;">// Get first record</span>
&nbsp;
    <span style="color: #0000ff;">print</span> custTable.<span style="color: #000000;">&#40;</span>fieldId<span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// Print account number</span>
&nbsp;
    <span style="color: #0000ff;">select</span> custTable
        <span style="color: #0000ff;">where</span> custTable.<span style="color: #000000;">&#40;</span>fieldId<span style="color: #000000;">&#41;</span> <span style="color: #00007f;">==</span> <span style="color: #ff0000;">'1101'</span>;  <span style="color: #007f00;">// Where clause based on field ID</span>
&nbsp;
    <span style="color: #0000ff;">print</span> custTable.<span style="color: #000000;">Name</span>;
    <span style="color: #0000ff;">pause</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It&#8217;s possible to use field IDs to retrieve values as well as use them in a where clause.  Again parentheses are the key.  Instead of writing a regular field name, wrap the ID in parentheses and you&#8217;re done.  This makes it possible create generic code to handle any table and field without knowing the types at compile time.  If you dig around in the classes for importing and exporting data you&#8217;ll find some examples.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/lHdqUyXRv6I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/tricks-with-xpp-embedded-sql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/tricks-with-xpp-embedded-sql/</feedburner:origLink></item>
		<item>
		<title>Constructing date values</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/bQB5x11dUTs/</link>
		<comments>http://sysdictcoder.com/blog/constructing-date-values/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 10:09:02 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Best practice]]></category>
		<category><![CDATA[Dynamics Ax (Axapta)]]></category>
		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/?p=84</guid>
		<description><![CDATA[Sometimes you have to construct at date at runtime with part of the date based on user input.  Like when you have to summarize transactions for a given year.
Ax has a built-in function for this but all too often I see people resorting to convoluted string conversions to achieve the goal.  Please don&#8217;t, [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you have to construct at date at runtime with part of the date based on user input.  Like when you have to summarize transactions for a given year.</p>
<p>Ax has a built-in function for this but all too often I see people resorting to convoluted string conversions to achieve the goal.  Please don&#8217;t, it&#8217;s harder to read, more error prone and less efficient.  Use <em>mkDate()</em> instead.</p>
<p>So instead of writing something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Job1<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0000ff;">int</span> givenYear <span style="color: #00007f;">=</span> <span style="color: #000000;">2009</span>; <span style="color: #007f00;">// Value acquired somewhere outside the method</span>
&nbsp;
    <span style="color: #0000ff;">date</span> fromDate<span style="color: #00007f;">,</span> toDate;
    ;
&nbsp;
    fromDate    <span style="color: #00007f;">=</span> <span style="color: #0000ff;">str2date</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;1/1/&quot;</span> <span style="color: #00007f;">+</span> int2str<span style="color: #000000;">&#40;</span><span style="color: #000000;">2009</span><span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span> <span style="color: #000000;">123</span><span style="color: #000000;">&#41;</span>;
    toDate      <span style="color: #00007f;">=</span> <span style="color: #0000ff;">str2date</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;1/1/&quot;</span> <span style="color: #00007f;">+</span> int2str<span style="color: #000000;">&#40;</span><span style="color: #000000;">2009</span><span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span> <span style="color: #000000;">123</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Write this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Job1<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0000ff;">int</span> givenYear <span style="color: #00007f;">=</span> <span style="color: #000000;">2009</span>;  <span style="color: #007f00;">// Value acquired somewhere outside the method</span>
&nbsp;
    <span style="color: #0000ff;">date</span> fromDate<span style="color: #00007f;">,</span> toDate;
    ;
&nbsp;
    fromDate    <span style="color: #00007f;">=</span> <span style="color: #0000ff;">mkDate</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #00007f;">,</span> <span style="color: #000000;">1</span><span style="color: #00007f;">,</span> givenYear<span style="color: #000000;">&#41;</span>;
    toDate      <span style="color: #00007f;">=</span> <span style="color: #0000ff;">mkDate</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">31</span><span style="color: #00007f;">,</span> <span style="color: #000000;">12</span><span style="color: #00007f;">,</span> givenYear<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To me, the most important advantage of mkDate() is clarity.  Using string conversions distracts from the actual task of constructing a date.  Additionally because all arguments to mkDate() are integers it&#8217;s easy to do input validation or build a whole bunch of date values in a loop, e.g. every first of every month.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/bQB5x11dUTs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/constructing-date-values/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/constructing-date-values/</feedburner:origLink></item>
		<item>
		<title>Detecting default values</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/tbLTWmpSEbo/</link>
		<comments>http://sysdictcoder.com/blog/detecting-default-values/#comments</comments>
		<pubDate>Thu, 21 May 2009 08:01:53 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Dynamics Ax (Axapta)]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/?p=43</guid>
		<description><![CDATA[So I haven&#8217;t posted in a long time.  I finally fixed some technical issues on the blog and upgraded to a recent version of Wordpress.  Let&#8217;s start with a quick tip to warm up :)
As you know X++ supports methods with default values.  In Ax it&#8217;s possible to detect when such a [...]]]></description>
			<content:encoded><![CDATA[<p>So I haven&#8217;t posted in a long time.  I finally fixed some technical issues on the blog and upgraded to a recent version of Wordpress.  Let&#8217;s start with a quick tip to warm up :)</p>
<p>As you know X++ supports methods with default values.  In Ax it&#8217;s possible to detect when such a default value was used at runtime with <em>prmIsDefault()</em>.  To see this for yourself, create a class with a method like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> someMethod<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #00007f;">=</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ;
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">prmIsDefault</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        info<span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'Default'</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000ff;">else</span>
    <span style="color: #000000;">&#123;</span>
        info<span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'Value given'</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And call it from another method.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">    o.<span style="color: #000000;">someMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    o.<span style="color: #000000;">someMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Even though the value is the same in both cases a different info message will be displayed.</p>
<p>Now you may be wondering how this can be useful.  Some scenarios:</p>
<ul>
<li>Avoiding expensive operations if the default value was used</li>
<li>Avoiding unwanted initializations</li>
<li>Dealing with customizations that require extra parameters in standard Ax classes</li>
</ul>
<p>If you do a search for prmIsDefault() in the AOT you will see it&#8217;s used often.  Classes with the Ax prefix are the most obvious example. Because getters and setters are rolled into a single method it&#8217;s necessary to know if the method was used as a getter or a setter and keep track of modified fields.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/tbLTWmpSEbo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/detecting-default-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/detecting-default-values/</feedburner:origLink></item>
		<item>
		<title>Changing properties of AOT objects in code</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/GRK_mVXx8Ag/</link>
		<comments>http://sysdictcoder.com/blog/changing-properties-of-aot-objects-in-code/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 08:51:33 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Dynamics Ax (Axapta)]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/changing-properties-of-aot-objects-in-code/</guid>
		<description><![CDATA[A while ago I wrote about reflection in Dynamics Ax.  In the article I mentioned that it was also possible to modify the AOT and that I&#8217;d give some examples.  Well, I kind of forgot.  Sorry about that.  Todd Hensley was kind enough to remind me of that promise and presented [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I wrote about <a href="http://sysdictcoder.com/blog/reflection-with-the-dictionary/">reflection in Dynamics Ax</a>.  In the article I mentioned that it was also possible to modify the AOT and that I&#8217;d give some examples.  Well, I kind of forgot.  Sorry about that.  Todd Hensley was kind enough to remind me of that promise and presented an interesting example problem.</p>
<blockquote><p>
I have a list of tables where I want to set the ClusterIndex to be the same as the PrimaryIndex.</p>
<p>I&#8217;ve been able to loop through an array of the table names, look them up using DictTable, and examine the PrimaryIndex and ClusterIndex values.</p>
<p>But I can&#8217;t figure out how I would say:<br />
myTable.clusterIndex = myTable.primaryIndex;</p>
<p>The properties all appear to be read-only.</p>
<p>Any ideas?
</p></blockquote>
<p>Thank you for the question and, yes, I do have an idea.</p>
<h3>Enter the TreeNode</h3>
<p>The key to solving this is knowing that there is more than one way to do reflection.  As Todd found out, the Dictionary API is read-only.  But the TreeNode API is not.</p>
<p>TreeNode is exactly what its name says: a node in a tree.<br />
I can hear you thinking: &#8220;Nodes? Tree? What are you talking about?&#8221;</p>
<p>I&#8217;m talking about the Application Object Tree.  The AOT has a bunch of nodes and a TreeNode object can represent any one of those nodes, like Forms, a class method or a table.</p>
<p>Whenever you&#8217;re using the AOT, clicking on an object and setting properties you&#8217;re using TreeNodes.  And the best part is you can do it in code too!</p>
<h3>Getting the node</h3>
<p>Enough talk.  Time to show the code.  First you need to get hold of a node.  Let&#8217;s use CustTable as an example.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">    TreeNode    node <span style="color: #00007f;">=</span> TreeNode<span style="color: #00007f;">::</span><span style="color: #000000;">findNode</span><span style="color: #000000;">&#40;</span>@<span style="color: #ff0000;">'<span style="color: #000000;">\D</span>ata dictionary<span style="color: #000000;">\T</span>ables<span style="color: #000000;">\C</span>ustTable'</span><span style="color: #000000;">&#41;</span>;
    ;
    info<span style="color: #000000;">&#40;</span>node.<span style="color: #000000;">treeNodePath</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    info<span style="color: #000000;">&#40;</span>node.<span style="color: #000000;">AOTname</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>As you can see, you need to enter the path similar to the way you open the nodes AOT.  If findNode() fails it will return null.</p>
<p>Once you have the node the fun starts.  In this case I&#8217;m just dumping something in the infolog to show you it works.</p>
<h3>Properties</h3>
<p>Now let&#8217;s do what we really want.  We&#8217;re going to set the primary index as the cluster index.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">    TreeNode    node <span style="color: #00007f;">=</span> TreeNode<span style="color: #00007f;">::</span><span style="color: #000000;">findNode</span><span style="color: #000000;">&#40;</span>@<span style="color: #ff0000;">'<span style="color: #000000;">\D</span>ata dictionary<span style="color: #000000;">\T</span>ables<span style="color: #000000;">\C</span>ustTable'</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #0000ff;">str</span> indexName;
&nbsp;
    #Properties
    ;
&nbsp;
    indexName <span style="color: #00007f;">=</span> node.<span style="color: #000000;">AOTgetProperty</span><span style="color: #000000;">&#40;</span>#PropertyPrimaryIndex<span style="color: #000000;">&#41;</span>;
    node.<span style="color: #000000;">AOTsetProperty</span><span style="color: #000000;">&#40;</span>#PropertyClusterIndex<span style="color: #00007f;">,</span> indexName<span style="color: #000000;">&#41;</span>;
    node.<span style="color: #000000;">AOTsave</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    node.<span style="color: #000000;">treeNodeRelease</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    node <span style="color: #00007f;">=</span> <span style="color: #0000ff;">null</span>;</pre></div></div>

<p>There are a couple of things to explain here.  First of all the macro #Properties contains the names of all properties you can read and write.  These are the same as what you get in the property window.  Use the macro instead of literal strings, it&#8217;s safer and doesn&#8217;t violate best practices.</p>
<p>Next you can see there is a set of methods to get and set properties.  After modifying anything you need to save the changes, just like you would when changing an AOT object in the property window.  TreeNode works just like doing it manually.</p>
<p>Finally you need to release the memory held by the TreeNode.  The garbage collector cannot clean up TreeNode objects by itself.  It needs a little help from you by calling <em>treeNodeRelease()</em> when you&#8217;re done.  In this example nothing bad would happen if you don&#8217;t.  However, if you run this in a loop a few thousand times Ax will run out of memory.  Not something you really want.</p>
<h3>Fitting it in with DictTable</h3>
<p>Todd already did a lot of work with DictTable to find the tables he needs to change.  And now I tell him DictTable won&#8217;t cut it.    Not really helping.</p>
<p>Not all is lost.  DictTable has a method to get a TreeNode object.  How convenient.  If we change the above example to start with a DictTable object we end up with this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">    SysDictTable    dictTable;
    TreeNode        node;
&nbsp;
    <span style="color: #0000ff;">str</span> indexName;
&nbsp;
    #Properties
    ;
&nbsp;
    dictTable <span style="color: #00007f;">=</span> SysDictTable<span style="color: #00007f;">::</span><span style="color: #000000;">newTableId</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">tableNum</span><span style="color: #000000;">&#40;</span>CustTable<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    node <span style="color: #00007f;">=</span> dictTable.<span style="color: #000000;">treeNode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    indexName <span style="color: #00007f;">=</span> node.<span style="color: #000000;">AOTgetProperty</span><span style="color: #000000;">&#40;</span>#PropertyPrimaryIndex<span style="color: #000000;">&#41;</span>;
    node.<span style="color: #000000;">AOTsetProperty</span><span style="color: #000000;">&#40;</span>#PropertyClusterIndex<span style="color: #00007f;">,</span> indexName<span style="color: #000000;">&#41;</span>;
    node.<span style="color: #000000;">AOTsave</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    node.<span style="color: #000000;">treeNodeRelease</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    node <span style="color: #00007f;">=</span> <span style="color: #0000ff;">null</span>;</pre></div></div>

<p>This can be adapted to fit in a loop over several tables and Todd&#8217;s problem is solved.</p>
<p>You can do a lot more fun stuff with the TreeNode API.  I&#8217;ll cover that in another installment.  I won&#8217;t forget this time.  I promise :)</p>
<p>Also, if you have questions or ideas for articles don&#8217;t hesitate to <a href="http://sysdictcoder.com/blog/contact/">let me know</a>.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/GRK_mVXx8Ag" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/changing-properties-of-aot-objects-in-code/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/changing-properties-of-aot-objects-in-code/</feedburner:origLink></item>
		<item>
		<title>A quick fix for unbalanced TTS</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/fY4kPRnXbVY/</link>
		<comments>http://sysdictcoder.com/blog/a-quick-fix-for-unbalanced-tts/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 13:35:50 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Dynamics Ax (Axapta)]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/a-quick-fix-for-unbalanced-tts/</guid>
		<description><![CDATA[During development you may encounter one of these.

This usually leaves your Ax session in an unusable state that you can&#8217;t close properly.  However, there&#8217;s no need to get out the big guns and kill the process with the Task Manager.
Instead, if possible, open the AOT and run this job:

static void resetTTS&#40;Args _args&#41;
&#123;
   [...]]]></description>
			<content:encoded><![CDATA[<p>During development you may encounter one of these.<br />
<a href='http://sysdictcoder.com/blog/wp-content/uploads/unbalanced-tts-error.png' title='Unbalanced TTS error'><img src='http://sysdictcoder.com/blog/wp-content/uploads/unbalanced-tts-error.png' alt='Unbalanced TTS error' /></a></p>
<p>This usually leaves your Ax session in an unusable state that you can&#8217;t close properly.  However, there&#8217;s no need to get out the big guns and kill the process with the Task Manager.</p>
<p>Instead, if possible, open the AOT and run this job:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> resetTTS<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0000ff;">while</span> <span style="color: #000000;">&#40;</span>appl.<span style="color: #000000;">ttsLevel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #00007f;">&gt;</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0000ff;">ttsAbort</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It simply rolls back any pending transactions until the TTS level is back at zero.  Now, this doesn&#8217;t fix the cause of the problem but it makes life easier trying to iron out the bug.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/fY4kPRnXbVY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/a-quick-fix-for-unbalanced-tts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/a-quick-fix-for-unbalanced-tts/</feedburner:origLink></item>
		<item>
		<title>Dealing with containers</title>
		<link>http://feedproxy.google.com/~r/sysdictcoder/~3/lPgVEAVcxaY/</link>
		<comments>http://sysdictcoder.com/blog/dealing-with-containers/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 11:10:40 +0000</pubDate>
		<dc:creator>Björn</dc:creator>
				<category><![CDATA[Dynamics Ax (Axapta)]]></category>
		<category><![CDATA[X++]]></category>

		<guid isPermaLink="false">http://sysdictcoder.com/blog/dealing-with-containers/</guid>
		<description><![CDATA[The X++ container is a powerful and somewhat odd datatype.  It&#8217;s like an array but supports different data types in the same instance and it can grow infinitely large.  Also containers don&#8217;t have methods like real objects; you need to call special functions and pass your container to them.
Many data structures in X++ [...]]]></description>
			<content:encoded><![CDATA[<p>The X++ container is a powerful and somewhat odd datatype.  It&#8217;s like an array but supports different data types in the same instance and it can grow infinitely large.  Also containers don&#8217;t have methods like real objects; you need to call special functions and pass your container to them.</p>
<p>Many data structures in X++ are serialized (packed) into containers before they&#8217;re sent across the network or stored in the database.  The entire SysLastValue framework runs on it.</p>
<p>Unfortunately this kind of flexibility is often abused.  It&#8217;s very easy to abuse containers and I see it happen too often.  In this article I&#8217;ll go over some  good practices for containers.  I won&#8217;t go into basics of containers.  You can find basic documentation on <a href="http://msdn2.microsoft.com/en-us/library/aa856741.aspx">MSDN</a> (methods starting with <em>con</em>).</p>
<h3>Appending to containers</h3>
<p>Most of the time you want to append something to a container.  Typically I come across code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i<span style="color: #00007f;">=</span><span style="color: #000000;">1</span>; i<span style="color: #00007f;">&lt;=</span>maxNum; i<span style="color: #00007f;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    con <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conIns</span><span style="color: #000000;">&#40;</span>con<span style="color: #00007f;">,</span> i<span style="color: #00007f;">,</span> theValue<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Don&#8217;t do that.  The conIns() function is slow because it copies the original container to a new one before returning.</p>
<p>The above should be rewritten as:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>i<span style="color: #00007f;">=</span><span style="color: #000000;">1</span>; i<span style="color: #00007f;">&lt;=</span>maxNum; i<span style="color: #00007f;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    con <span style="color: #00007f;">+=</span> theValue;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It&#8217;s a lot faster and easier to read as well.</p>
<p>Only use conIns() if there&#8217;s no other option, i.e. for inserting values not at the end.  Even adding elements to the front of the container is slow because the existing container is still copied.  If at all possible write your code in such a way that you only append elements using +=.  This is the fastest way to do it as it modifies the original container in place. And it&#8217;s easier to read as well.</p>
<h3>Initialising</h3>
<p>Whenever you&#8217;re going to insert a fixed set of variables into the container you can add them all at once.  A typical example when writing something to a file.</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>someCondition<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    con <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    con <span style="color: #00007f;">+=</span> <span style="color: #000000;">1</span>;
    con <span style="color: #00007f;">+=</span> someValue;
    con <span style="color: #00007f;">+=</span> otherValue;
    con <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">&quot;X&quot;</span>;
    outFile.<span style="color: #000000;">write</span><span style="color: #000000;">&#40;</span>con<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is works but there&#8217;s a better way.  You can assign a bunch of values to the container in a single line.  Try this instead:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>someCondition<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     con <span style="color: #00007f;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #00007f;">,</span> someValue<span style="color: #00007f;">,</span> otherValue<span style="color: #00007f;">,</span> <span style="color: #ff0000;">&quot;X&quot;</span><span style="color: #000000;">&#93;</span>;
     outFile.<span style="color: #000000;">write</span><span style="color: #000000;">&#40;</span>con<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is a contrived example and often the complexity filling the container can be pushed into a separate method.  Usually hiding details like that increases readability of the code.  It isn&#8217;t always possible to do it like this but simplify your code whenever possible.</p>
<h3>Extracting values</h3>
<p>The above trick works the other way around too.  You can write this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;">a <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con<span style="color: #00007f;">,</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>;
b <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con<span style="color: #00007f;">,</span> <span style="color: #000000;">2</span><span style="color: #000000;">&#41;</span>;
c <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conPeek</span><span style="color: #000000;">&#40;</span>con<span style="color: #00007f;">,</span> <span style="color: #000000;">3</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>This can be done in a simpler way:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>a<span style="color: #00007f;">,</span> b<span style="color: #00007f;">,</span> c<span style="color: #000000;">&#93;</span> <span style="color: #00007f;">=</span> con;</pre></div></div>

<p>Bonus points if you can explain this:</p>

<div class="wp_syntax"><div class="code"><pre class="xpp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>a<span style="color: #00007f;">,</span> b<span style="color: #000000;">&#93;</span> <span style="color: #00007f;">=</span> <span style="color: #000000;">&#91;</span>b<span style="color: #00007f;">,</span> a<span style="color: #000000;">&#93;</span>;</pre></div></div>

<h3>Passed by reference</h3>
<p>Containers are a primitive data type in Ax, so they&#8217;re passed by value.  That means containers will be copied when passed as a function argument.  Keep that in mind when you&#8217;re dealing with containers that can become large.  For small containers the performance hit is negligible but at some point you&#8217;re going to notice it.</p>
<h3>Don&#8217;t mimic other data structures with containers</h3>
<p>Too often I see a mess of containers that could be replaced by a much more appropriate data structure.  </p>
<p>Although it&#8217;s very easy and tempting to use containers for everything, again, it&#8217;s not a good idea.  Other developers may have a hard time understanding these ad hoc data structures.  Even understanding your own code can be a problem if you haven&#8217;t worked on it for a while.</p>
<p>There are alternatives to containers.  Take a look at the Foundation Classes.  They&#8217;re somewhat like the .NET collections.  You can use a List, Map, Set, Array and Struct.  They can be packed into containers when necessary.  </p>
<p>If all variables are of the same type, consider using a List (ordered) or Set (unordered).  If you need to track related values use Maps (key/value pairs) or Structs (supports different complex types).  Please don&#8217;t use nested containers or keep values together in several containers at the same index.  It&#8217;s hard to read and easy to break.</p>
<img src="http://feeds.feedburner.com/~r/sysdictcoder/~4/lPgVEAVcxaY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sysdictcoder.com/blog/dealing-with-containers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://sysdictcoder.com/blog/dealing-with-containers/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 3.369 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-09-07 08:40:47 -->
