Wednesday, June 24, 2015

Dynamics GP - watch out for using "CHANGE" when you really meant tot use "CHG"

Consider the code:

in string RecordID;
inout table RecordTable;
optional in integer Op = GET;
assert Op = GET or Op = CHG;
{the rest goes on to get or change the table}

What happens if the caller passes in CHANGE? It'll do a get. Why? Because the constants GET = CHANGE. The assert attempts to protect against this mix up, but can't, because when you pass in CHANGE Op=GET is true, and therefore the assert succeeds.

If you accidentally use CHANGE instead of CHG, you'll run into runtime issues. A workaround to get the compiler to catch this would be to use string constants instead of integers.

For example, if I added the constants sGET="GET" and sCHG="CHG", I could then change the code to the following:
in string RecordID;
inout table RecordTable;
optional in string Op = sGET;
assert Op = sGET or Op = sCHG;
{the rest goes on to get or change the table}

this would prevent the problem of passing in "CHANGE", because if you tried to pass in CHANGE you'd get a compiler error since CHANGE is an integer constant and it's expecting a string. 

Monday, June 8, 2015

.NET interface to rasdial

For my future reference, this library makes it easy to dial VPN and dial-up connections.

DotRas link here:
http://dotras.codeplex.com/

Start new thread using lambda expression and anonymous method

Old way
Thread thread = new Thread(doStuff);
thread.Start();

void doStuff()
{
    //do lots of stuff
}

Using lambda expression
Task.Factory.StartNew(() => doStuff());

void doStuff()
{
    //do lots of stuff
}

That's a little easier, but I still have to make a separate method (doStuff) just to do stuff in another thread.

Anonymous method
Task.Factory.StartNew(() =>
          { //cut and paste doStuff()'s body in here
               //do lots of stuff
          }

);

Sweet, now I don't need to add a new method just to do stuff in a new thread.
There was an error in this gadget