Thanks for the feedback.   I have looked at the IFormatProvider and it will 
work for what I am doing.  However, it's not the problem that I am having.  The 
problem that I am having is finding a place to store the format string for each 
variable I declare.  I could pass it into my function.  That is an option.  
However, the format string never changes during the whole application.  It is 
set at compile time and never changes.  So add thousands of extra bits of code 
just to hard code the format string for each variable seems like over kill.  
I'm looking for a simpler example.  

Here's an example of what I would have to do without solving this problem.  
Keep in mind that the real application is about 5,000 programs at an averaging 
between 1,000 to 10,000 lines of code each.  So if I do not solve this in a 
nice way, I will have to work through 5 to 10 million lines of code.  So going 
through and rewriting the code is not an option.  What I can do is write 
find/replace types of logic to adjust the code as needed.    

'Global Declarations
Dim int1 as Integer = 0               'db field length is 1     
Dim int2 as Integer = 12      'db field length is 5
Dim int3 as Integer = 111     'db field length is 3
Dim int4 as Integer = 0               'db field length is 7
Dim int5 as Integer = 1234567 'db field length is 15
Dim int6 as Integer = 123456  'db field length is 5
Dim int7 as Integer = 1234    'db field length is 1
Dim int8 as Integer = 1234    'db field length is 2

'Set Formats before pushing data into database
Dim str1 as string = format(int1, "0")
Dim str2 as string = format(int2, "00000")
Dim str3 as string = format(int3, "000")
Dim str4 as string = format(int4, "0000000")
Dim str5 as string = format(int5, "000000000000000")
Dim str6 as string = format(int6, "00000")
Dim str7 as string = format(int7, "0")
Dim str8 as string = format(int8, "00")

'The existing code would look something like the following.  Keep in mind that 
this sort of code is repeated hundreds of times in each program, and that there 
are thousands of programs.  So my solution needs to have something that is 
pretty strait forward and use a lot of code reuse.  The design needs to be 
pretty elegant simply based on the volume of code I am dealing with.
  
Dim str1 as string = int1
Dim str2 as string = int2
Dim str3 as string = int3
Dim str4 as string = int4
Dim str5 as string = int5
Dim str6 as string = int6
Dim str7 as string = int7
Dim str8 as string = int8

'The problem is that it needs to be correctly formatted, which is what the 
previous example uses.

I can do something like the following using extensions or the IFormatProvider.  
But that doesn't buy me much.  I basically have to hard code the format string 
into every line of code that uses the variable.  Of course I could hard code a 
global and use that in each line of code, but that also doesn't buy me much.

        Dim str1 as string = int1.FormatString("0")

But what I really want to do is something like the following and have it 
automatically pickup the format.

        Dim str1 as string = int1
        Or
        str1 = int1   

I figured that I could extend .ToString() and add my own logic to handle the 
format.  But I do not see how to extend .ToString().  

I could go ahead and add code to do something like...

        Dim str1 as string = int1.FormatString()

This is not the best option, but at least I am not pushing the format string as 
well.  But then I would have to look up the format string from somewhere.  
Within a FormatString() extension logic I can do that.  But I need a way to 
look it up... from some store of format and variable name.  I can build an 
array such as...

"int1", "0"
"int2", "00000"
"int3", "000"
"int4", "0000000"
etc.

Now when I call the format string extension, I can look up the given variable's 
format string, then format it according.  But the problem is, how to I get the 
variable's name from the variable object?  Is there a way to do that?

Dim int1 as Integer = 0
Dim str1 as String = FormatString(int1)

Public Function FormatString(byVal value as integer) as String
        'get value's format string from the area.
        '? what is the name of value so I can look it up in the area?
        'The name to look up is "int1", but how to I pull that from value?  Can 
I?
End Function


If you are still not following me, then basically I need to declare a variable, 
such as 
        Dim int1 as Integer = 0

Then I need to be able to use it in a function
Dim str1 as String = FormatString(int1)

When it's called in the fuction, here's a sample function, I need to be able to 
get the real name of int1.

        Public Function FormatString(byVal value as integer) as String
                Dim name as string = Value.name 'so how do I get value's actual 
name?  Is that possible?                

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to