Categories
Bizagi Tips and Tricks

Using String Format in Bizagi

The String Format method can be really useful while coding in Bizagi. In my experience I’ve used the method in the following situations:

  • Insert values into a text
  • Format the text

Insert text/Replace text

A good example of using the String.Format method is the customised message I display to the end-user. This could be a warning message that comes up if a form validation fails. E.g the entered value is lower than the minimum accepted value. Here is an example of how the code looks like:

var iValue = <iCustomerAge>;
var iMinimumValue = 18;
var sMessage = String.Format("The value you have entered ({0}), is lower than the minimum accepted. Please enter a value higher than {1}", iValue, iMinimumValue);
CHelper.ThrowValidationAlert(sMessage);

Text Format

In recent days I had a request to display the value of a large number in a particular format. E.g to use comma(,) as a separator for thousands and dot(.) for decimals. Also, the value was supposed to have a maximum of 2 decimals. To achieve this I have used String.Format.

<sFormatedValue> = String.Format("{0:#,##0.##}", 112342.457);
// --- outcome: 112,342.45

If you are interested in more custom numeric formats have a look at this Microsoft article.

Another easy solution is to use the ToString method, but in my particular case, because of the Bizagi version, that method didn’t work.

Also, in some versions of Bizagi, I had issues with the String.Format method but I managed to fix it by adding the System.String.Format. The good thing is that the error message appears when you try to save the expression and not when you run the web application. So, if you get an error saying that the String.Format is not recognised, add System in front of it.