Category Archives: Programming

Working around history.back() bug in BlackBerry Torch browser widget

I’m on the team working on Pylon, a smartphone app which allows the user to approve Nintex tasks, among other things. It currently uses BlackBerry Webworks, which is a platform that uses HTML and JavaScript in the UI. On one screen we wanted functionality that would send the user back to the previous screen (as if they clicked the back button). The way to do this in JavaScript is:

history.go(-1);

or

history.back();

However, in my testing this did nothing on the BlackBerry Torch running OS 6, or on the Torch simulator. I found a workaround which works on most OS 5 and 6 devices (see Caveat for the exception.)

Basically, I needed to write a Webworks extension function to trigger the behavior from Webworks. Continue reading Working around history.back() bug in BlackBerry Torch browser widget

System.BadImageFormatException on a 64-bit Machine

The other day, one of our customers was switching over to 64-bit development machines, and encountered the following issue:

System.BadImageFormatException:

Could not load file or assembly ‘(assembly name)’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

The likely reason is that you (or in this instance, our customer) are trying to load a 32-bit assembly into a 64-bit application. This can happen when you have the Any CPU platform selected.

Solution:

If your scenario sounds like the one I described, try setting your application to run in x86 mode. In Visual Studio this can be done by going to Build -> Configuration Manager…

…and setting the Platform to be x86.

CRM 4.0: Coverage Dates Contract Acts Like Number of Cases Contract

Lately we’ve discovered an issue with Contracts not allowing users to create an unlimited number of Cases against them, even though the template of the Contracts was set as Coverage Dates.
The behavior could be described as follows:
An active Contract with contract template type Coverage Dates will show contracts lines with the “available number of cases” value set to 1.  As soon as you create and resolve one Case, the “remaining cases” value becomes 0. As a result, no more Cases can be created against the Contact.
To figure out where the issue is coming from and how to fix it – we looked at the way we create our Contracts and what is involved in the process.

Let’s say you’re using a heavily customized Microsoft Dynamic CRM. One of the business requirements you may come across is the need to generate Contracts programmatically. This need might arise via the necessity of integrating CRM with other legacy systems you have or simply to ease the life of your customer service reps by letting them create new Contracts in one click. Continue reading CRM 4.0: Coverage Dates Contract Acts Like Number of Cases Contract

Static Initialization in the BlackBerry JVM

The Problem

I was investigating some odd behavior on the BlackBerry recently to try to diagnose a problem. Along the way, I discovered a bug in BlackBerry’s JVM, as a result of which static initialization can occur multiple times – and even static final variables can change.

Consider the following code:

public class App extends Application
{
	public static void main(String[] args)
	{
		System.out.println(BaseClass.class.getName());
		System.out.println(Subclass.class.getName());
	}
}

public class BaseClass
{
	static
	{
		System.out.println("Static initialization called");
	}
}

public class Subclass extends BaseClass
{

}

Continue reading Static Initialization in the BlackBerry JVM

Numbers in Emacs

found this page: http://www.emacswiki.org/emacs/NumbersInRegisters

Registers are useful tools when writing KeyboardMacros. Notice how you can store numbers in registers, insert them again, and increment them in the register.

See the Node Keeping Numbers in Registers in the EmacsManual.

Example session:

‘C-u 1000 C-x r n x’
Store 1000 in register x
`C-x (‘
Start a keyboard macro
‘C-x r i x’
Insert the number in register x into the buffer
`C-x r + x’
Increment the number by one
`<end> <newline>
Do something else
`C-x )’
End keyboard macro definition

Now run ‘C-u 50 C-x e’ to run the macro 50 times.

See IncrementNumber, ReplaceRegexp, ReplaceCount or RenumberList for alternative that don’t involve registers. Continue reading Numbers in Emacs

Removing Character from String in C Sharp

I need to process a string in order to remove certain unwanted characters. One such character has an ASCII value of Hex 01. I happen to be using a StringBuilder, which offers a Replace method similar to String.Replace.

I can’t simply do a Replace(Convert.ToString(0x1), "") which just removes all instances of the string “1”. To get the correct character, I can use Convert.ToChar(0x1) or simply ‘\x01’. However, I can’t remove a character with the Replace(char, char) overload because there is no such thing as an “empty” character literal that’s analogous to an empty string. Each value of char type is mapped to a code point and is therefore exactly one character long.

To remove the character, simply take an additional step and convert it to a string: Replace('\x01'.ToString(), "").

Switching on Strings in C++ and C

I was wondering what the performance hit of switching on strings was. This is interesting to know:

http://dotnetperls.com/string-switch

In C++ and C they only allow switching on integral values. I imagine in certain cases the compiler could make a jump statement which used the integer directly (maybe as part of the jump address, positioned relatively from the current one). That’s probably why it’s not allowed for character arrays or objects in general in C++. At least that’s my guess.

Another interesting link: http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx

An interesting if unrelated link: http://blogs.ipona.com/james/archive/2008/05/23/Dangerous-Ideas-in-C-No.-1-A-Better-Switch.aspx

Refactoring XmlWriter

I found this link interesting: http://elegantcode.com/2009/06/19/refactoring-xmlwriter/

XmlWriter and XmlReader are convenient and one of the fastest ways of creating XML, but they are kind of ugly sometimes. I wrote a class that encapsulated these methods into XmlFilter, which makes it easy to change pieces of an XML file without writing the same scaffolding over and over again, and without using an XmlDocument (which is quite a bit slower and consumed more memory than I preferred.)

The next release of Officewriter should be significantly faster (roughly 2 to 4 times as fast, according to current benchmarks) than the 4.0 release for parsing Excel 2007 files. This is mostly because I replaced a section of code that used the XmlDocument with a custom written replacement which was more specific to my needs. I don’t think the .NET framework (at least parts of it) was written with performance in mind, but I’ll get into that in a later blog post.

Reverse Polish Notation

My knowledge of reverse polish notation came in handy today. It’s weird how these things come up in real life and not just in an academic setting. Granted, not every college graduate deals with the Excel file format.

Excel’s formulas are stored in binary spreadsheet files in reverse polish notation, which makes sense. There aren’t usually numbers in the file which say how many arguments a function contains, so we have to know that in advance to be able to understand the formula. You evaluate the arguments of a formula before the formula itself, so the stack at the point of evaluating the formula itself should contain exactly the number of arguments the formula has. It’s elegant when it works, but it makes it difficult to figure out when you have an error (unlike in XML where everything has boundaries)

Usually, if you read in more arguments than are available, an exception is thrown, and if you read in too few, an exception is thrown when evaluation completes and you have extra tokens to read. You could theoretically read too few arguments at one point and too many at another and just have an invalid result, but that would require two errors happening simultaneously, which is (hopefully) improbable.

Things I’d like in an IDE

I wish Resharper had a feature which pointed out where exceptions are being created but aren’t being thrown (ie, new Exception() instead of throw new Exception()). I also wanted a feature which automatically sets breakpoints where exceptions are thrown, although after thinking about that for a bit I’m not sure about that. I do really like the syntax checking for the most part, even if it messes up sometimes. And I still do go back to Emacs for anything repetitive; I haven’t seen anything in Resharper or VS that allows for Emacs-like macros.