Hi Greg,

If you are using .NET 3.5 you could use the extension methods feature of
the VB.NET/C# compiler.
Just build a formatting class that holds all of the formatting logic and
use it transparently on the types you need the formatting for.

'--------------------
Imports System.Runtime.CompilerServices

Module FormattingModule

  <Extension> _
  Public Shared Function ToFormattedString(ByVal value As Int32) As String
     ToFormattedString = value.ToString("0000")
  End Function

  <Extension> _
  Public Shared Function ToFormattedString(ByVal value As DateTime) As
String
     ToFormattedString = value.ToString("MMDDYYYY")
  End Function

End Module
'--------------------

Then use it within the rest of your code:

'--------------------
Console.WriteLine(123.ToFormattedString())
Console.WriteLine(DateTime.Now.ToFormattedString())
'--------------------

HTH

--
Efran Cobisi
http://www.cobisi.com

Greg Rothlander wrote:
The data types in .Net do not have a format string with them.  Is it
possible to extend the data types to add a format string?  I thought I came
across an example of this back a few years ago, but I do not recall.  It may
have been an example of how to create your own types.

Can anyone think of a way to extend the data types?  For example, let's say
that I want an Integer with a length of 5 and a format string of "00000".
Is there a way to do that?  The same might do for a date.  I might want to
set the format to "MMDDYYYY", but I would like to carry that within the data
type.

Why?  Because I have to set the format string on thousands of lines through
the program.  If I was able to store it on the data type itself, I wouldn't
have to do this.  If I could extend the data type I might be able to do
something like:

Dim X as Integer = 123
x.FormatString = "00000"
Dim str as string = X.ToString()

str would equal "00123"

I'm just tired of adding the format to EVERY line of code that needs the
length formatted to a 5 digits.  What I have to do now is for every line of
code were X is used, I have to do something like:

        whateverstring = Format(X, "00000")

I would rather just do something like:

        whateverstring = X.ToString() or maybe just whateverstring = X

It seems like there would be some way to do this.  It is very common in
other languages to have the format on the data type.  Dates a very common
thing to run into like this.  It would be nice to have the format of the
date already set by default.

It would also be nice to have an integer with a length that doesn't change.
For example, lets say that I pull something from a database with a field
length of 5 and it has a value of 123.  If I ask it the length, it will say
3.  But I need to know that the real length is 5.  Why?  Because the program
wants to store 00123 in the db column and not 123.  The 00 is important for
the application.

Any thoughts?  Am I missing something simple here and trying to work around
something that should not be worked around?  I do have some limitations
because the db needs the 00123 stored and with really mess up some of the
other programs that use the DB if I did store 123 in that field.  Why?
Other applications written in other languages also use the DB and the DB is
not SQL Server.

Any thoughts would be appreciated.

Best regards,
Jon

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

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




===================================
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