Custom String.Format

jswota

Well-known member
Joined
Feb 23, 2009
Messages
49
Programming Experience
Beginner
Hi,

I am developing an app that will import numeric data and export it to various text files using ascii templates. I am basically converting the template file into a single string, replacing application fields in the string with {0}, {1}, etc... and adding the result into an array that is then passed to the String.Format(str_Template, Params()) to provide the final output. I have set it up so the template will use the standard VB.Net formatting syntax.

A simple template example:
{<prt_Mark>:G} {<prt_Qty>:F2} {<prt_Length>:F2} {<prt_Remarks>:G}

If an app field is found in the string, the value is added to the Param() array and the field name {<prt_Mark>:G} is replaced with it's array position resulting in {0:G}.

The final template string will be:
{0:G} {1:F2} {2:F2} {3:G}

And the Params() array will be:
Params(0) = B1001
Params(1) = 10
Params(2) = 120.5
Params(3) = Remarks

The result of String.Format(str_Template, Params()) will be:
B1001 10 120.50 Remarks

This all works really well and i am very happy with the output flexibilty. The problem that i am facing now is a bit more challenging. There are going to be cases where we want to "tweak" the data upon export. The "tweaking" will not happen within the application and i am trying to figure out how to do some simple "tweaking" with the formatting. I have started researching the IFormattable interface but would like to know what people think before diving in.

The "tweaks" that might be required are simple replace and simple calculations. For instance: We might want the part mark to be B_1001 instead of B1001 or we might want the Length (120.50) to be the Length + 10 (130.50). I was thinking that i might be able to find a way to override the format method to include something like this:

{<prt_Length>:F2+10} or {<prt_Length>:F2*10} or {<prt_Mark>:G:Replace(B,B_)}

I guess i am really just wondering if there are already ways to perform calculations within the String.Format method?

I would really appreciate any thoughts or advice on this matter.

Thanks,
Joel
 
Hello.

I guess i am really just wondering if there are already ways to perform calculations within the String.Format method?
Why should it be? There would be no logic behind it, String.Format() is used to convert various Objects into a readable string...there's no need and no logic for calculations or manipulation of the objects there, especially 'cause you never know what kind of Object they are.

As I see it, you'll have to parse it. You could get rid of some work if you'd use reflection, f.e. extract the name of the methods and the parameters and call them through Reflection.
VB.NET:
{<prt_Mark>:G:<Method>(<Params>)}
Extract the Method Name and the Params from the string, and use Reflection toc all this method.

Bobby
 
String.Format(), when used, will call .ToString() of the object, passing in any format specifier found in the string:

e.g. if you have a class Foo, that implements its own ToString(language as String) and you say:

Dim myFoo as New Foo(1)
String.Format("foo is {0:french}", myFoo)


Then it will be the same as calling myFoo.ToString("french")

And then if your ToString took the "french" and returned "un" instead of "one" then you could change how it appears

Does this help? You will have to make a class for your PartNumber, rather than just having it as a string.
 
String.Format(), when used, will call .ToString() of the object, passing in any format specifier found in the string:

e.g. if you have a class Foo, that implements its own ToString(language as String) and you say:

Dim myFoo as New Foo(1)
String.Format("foo is {0:french}", myFoo)
This only works for the implementation of IFormattable.ToString.
Then it will be the same as calling myFoo.ToString("french")
Not quite the same, but you can do this also.
 
I must be going nuts; i'm sure the OP mentioned that he was playing with IFormattable but now I can't see the reference. Thanks for the supplement/correction, JohnH
 
Back
Top