Sit back, relax, and enjoy the code.

Quick Hits: Becareful when returning inc’ed vars

Posted: May 1st, 2010 | Author: abel | Filed under: MonoTouch, Programming, Quick Hits | Tags: , | 1 Comment »

I noticed this in some MonoTouch code that I wrote recently, but I think it makes sense across other languages as well.  Let’s say you write a method that returns an incremented variable like this:

private int MyAwesomeFunction(int pVar)
{
// coding magic
return pVar++;
}

…where pVar = 10, the returned value will NOT be 11!  The returned value will still be 10.  If you want to return an incremented value for pVar, do the following:

private int MyAwesomeFunction(int pVar)
{
// coding magic

return ++pVar; // or return pVar+1;
}

Just a case of postfix vs prefix.  In the previous example, the variable never gets incremented because the postfix operator would get evaluated after the line is processed, but that never happens because we’re returning the pVar.  By flipping to a prefix operator, we guarantee that we evaluate the opperator against the variable before we return it.

It’s also worth mentioning that being a little verbose here is also good.  Explicitly stating that you’re adding 1 to the variable (as displayed in the commented code) is the same number of characters and we eliminate the confusion over what’s getting returned.

Happy Coding!

  • Share/Bookmark

The 5 stages of 3.3.1

Posted: April 11th, 2010 | Author: abel | Filed under: MonoTouch | Tags: , , , | 5 Comments »

Developers have been up in arms on the news broken by Daring Fireball and TechCrunch.  The reported change in the iPhone developer agreement definitely stops Flash developers from creating iPhone apps using the new tools available CS5, but the sword Apple used also cuts into the heart of the MonoTouch, Appcelerator, Unity 3D, Corona, & PhoneGap communities.  Being a member of the MonoTouch community, I’m directly affected by this reported change.  Easily said, I’ve gone through the 5 stages of grief over the news:

Read the rest of this entry »

  • Share/Bookmark

Quick Hits: Localization strings in Monotouch

Posted: February 21st, 2010 | Author: abel | Filed under: MonoTouch | Tags: , | No Comments »

It’s pretty straight forward. Check out this post:

Internationalising my app

…and make sure the folders you use follow this format “[Language Abbreviation].lproj” as commenter #4 points out ;)

If you miss one of the major languages, your app will default to English.  As of this post, the iPhone OS supports the following languages:

English (U.S), English (UK), French (France), German, Traditional Chinese, Simplified Chinese, Dutch, Italian, Spanish, Portuguese (Brazil), Portuguese (Portugal), Danish, Swedish, Finnish, Norwegian, Korean, Japanese, Russian, Polish, Turkish, Ukrainian, Arabic, Thai, Czech, Greek, Hebrew, Indonesian, Malay, Romanian, Slovak, and Croatian

  • Share/Bookmark

I LOVE MonoTouch

Posted: January 24th, 2010 | Author: abel | Filed under: MonoTouch | Tags: , , | 1 Comment »

Don’t get me wrong, XCode is great…in it’s own way…but as a .Net dev coming to the iPhone platform, MonoTouch rocks my world!

For the other devs in my shoes looking to play with MonoTouch, here are some things to keep in mind: Read the rest of this entry »

  • Share/Bookmark