views:

40

answers:

2

I have some VB .NET software that interfaces to a load of old (but sound) COM objects. The VB provides a GUI for the COM objects, part of which consists of setting various options on the COM objects - several of which relate to string formatting.

I have a simple pair of VB .NET functions that convert basic %f, %d, %g formats to/from .NET equivalents using a large select case covering specific common strings, but they don't cover all formats. This is the kind of thing I have...

        ElseIf f = "%.3f" Then
            return "0.000"
        ElseIf f = "%.2f" Then
            return "0.00"
        ElseIf f = "%.1f" Then
            return "0.0"

Before I start diving in and making it more versatile with some parsing, does anyone know of a class (eg VB or C# .NET) that provides a decent ready-made implementation? Or perhaps some regexp wizadry could be used?

Many thanks

Tim

+1  A: 

Do you really need both formats, or is one format adopted by your users and the other is used inside the implementation details of your software -- but could go away if you had string formatting functions that recognized your users' options directly?

Ben Voigt
The .NET UI provides a dropdown of common formats, and tries to select the one that best matches the printf format in the underlying COM object (the above code is ok for that). However, if none matches I would like to show the user something other than a C++ format string, and this needs converting to/from the format shown to the user. I had thought that a .NET format string would be OK to show to the user, although perhaps there is a better solution?
ttt
Well, you could do it more like Microsoft Excel, where when you pull up the number formatting dialog you can firstly pick between General/Scientific/Percent/etc (e.g. `%f`, `%e`, etc) and then enter the number of decimal places (which corresponds directly to the printf-style format string as well). That's definitely more user-friendly than a printf format string, and probably moreso than a .NET format string. How many users would know the difference between `0.00`, `#.00`, and `0.0#`?
Ben Voigt
A: 

You don't need to. Windows has that formatting built-in. You can simply P/Invoke wsprintf from User32.dll.

MSalters