VBCorLib for VB6

Wednesday, December 21, 2005

My Favorite String Function...

So there I am typing away on a string literal. I have to include variable values within the string, so I unquote, hit ampersand, variable name, ampersand, and requote to continue with the string literal. Many times I'll need to format the variable value, too. And I have to do this for every variable I need. And if I use the same variable more than once, I have to type it more than once.

So what do I do now? I have become reliant on cString.Format to create the complex strings I need. Since cString is already publicly available, it's a simple process to insert values, formatted or not, duplicated or not without the quoting jargon.

I write error messages for my exceptions all the time. Usually I'll include a valid value or value range in the message. Now I automatically start string building with cString.Format.

Here is a typical exception message:
cString.Format("Valid values are from {0} to {1:n0}.", 1, 999999)

This outputs the following:
Valid values are from 1 to 999,999.

The cString.Format function uses an already declared StringBuilder and simply calls the AppendFormat function. The StringBuilder is cleared before each call.

So this provides me with a very quick, painless, and easy way to create my formatted strings. I don't think I could go back to the old way.

-Kelly

0 Comments:

Post a Comment

<< Home