Architecture Ques: FormatInfo class

T

TS

Hi, I had a microsoft guru help my team build a project. I noticed that in 2
situations where a value needed to be formatted in a certain way when viewed
in browser, he would create a new class called "format_nameFormatInfo".

He used 2 functions in each:
Public Function GetFormat(ByVal formatType As System.Type) As Object
Implements System.IFormatProvider.GetFormat

and

Public Function Format(ByVal formatString As String, ByVal argToBeFormatted
As Object, ByVal formatProvider As System.IFormatProvider) As String
Implements System.ICustomFormatter.Format


I'm wondering if this is a common practice for all OO gurus. The reason I
ask is I have to do the same thing in other parts of the app, and also
becuase I just want to know the preferred way to doing things.

Thanks.
 
S

Steven Cheng[MSFT]

Hi TS,


Thanks for posting in the community!
From your description, your team has defined a certain custom data/string
formatting class which follow the IFormatProvider/ICustomFormatter pattern
in the dotnet framework. And you wonder whether this is a most popular
pattern in all oo area, yes?

Based on my experience, the Custom formatting interfaces in dotnet are all
well designed based on the existing OO formatting practices. The
System.IFormatProvider interface gives you a way to tell the framework that
your custom class is capable of formatting strings. It is commonly combined
with the ICustomFormatter interface, which defines the method to actually
format the string.

#IFormatProvider Interface
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemiformatprovide
rclasstopic.asp?frame=true

As the ICustomFormatter, the ICustomFormatter interface gives you a way to
implement your own string formatting routines for classes that you cannot
change the formatting of directly. For example, you might want to implement
the ICustomFormatter interface to let you format DateTime objects in your
own special way. This is in comparison to objects that you have the source
for, in which case you would implement the IFormattable interface.The
IFormatProvider interface defines a single method, GetFormat.

MSDN has a good article on customizing format strings. The section, "Adding
Custom Format Strings to Existing Types" applies to the ICustomFormatter
interface.

#Customizing Format Strings
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcustomizingformats
trings.asp?frame=true

Please have a view on the above item. Hope this helpful.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top