IFormatProvider

  • Thread starter Thread starter Que
  • Start date Start date
Q

Que

Hi

The I wrote the following Code

If Not txtInvoice.Text = "" Then

txtInvoice.Text =
Convert.ToInt32(txtInvoice.Text()).ToString("000000")

End If

txtInvoice is a TextBox

I use FxCop and it says I should rewrite the code with the following
resolution

"frmInvoiceDetail.txtInvoice_Leave(Object, EventArgs)
:Void makes a call to
System.Convert.ToInt32(System.String)
that does not explicitly provide an IFormatProvider.
This should be replaced with a call to
System.Convert.ToInt32(
System.String,System.IFormatProvider)."

What does it mean that I should Provide an IFormatProvider.

What is IFormatProvider

How does this work

Excuse me for my ignorance

Thanks in Advance

Regards
AQ
 
Que,

This should be enough, I never know what character has to be for those
question marks.

Cint(txtInvoice.Text)ToString(????????)

Maybe you can try it yourself.

http://msdn.microsoft.com/library/d...mglobalizationnumberformatinfoclasstopic.aspI hope this helps,Cor"Que" <[email protected]> schreef in berichtHi>> The I wrote the following Code>> If Not txtInvoice.Text = "" Then>> txtInvoice.Text => Convert.ToInt32(txtInvoice.Text()).ToString("000000")>> End If>> txtInvoice is a TextBox>> I use FxCop and it says I should rewrite the code with the following> resolution>> "frmInvoiceDetail.txtInvoice_Leave(Object, EventArgs)> :Void makes a call to> System.Convert.ToInt32(System.String)> that does not explicitly provide an IFormatProvider.> This should be replaced with a call to> System.Convert.ToInt32(> System.String,System.IFormatProvider).">> What does it mean that I should Provide an IFormatProvider.>> What is IFormatProvider>> How does this work>> Excuse me for my ignorance>> Thanks in Advance>> Regards> AQ>
 
hi

thanks for the quick reply.
i do have that code,as you suggested
what i needed to know is how and why should IFormatProvider be
implimented and used

thanks again.
Que
 
An IFormatProvider provides culture specific information regarding how
dates, numbers etc are formatted (since different countries has different
rules). The Convert.ToInt32 method you are calling uses an IFormatProvider
that matches the settings you have in the regional settings in your control
panel. If you're getting input directly from the user this is most likely
the behaviour you want (I'm guessing that txtInvoice is a txtbox or
similar). If you need to transfer data between systems using different
country settings you need to use a common format for it so that each system
interprets it the same way (for example; 12.3 is not the same thing in
Sweden as it is in the US). That's when you specify a specific
IFormatProvider that uses the common rules that you need.

In your case it looks like FxCop is a bit overprotective. If you want to get
rid of the warning change your line to:
Convert.ToInt32(txtInvoice.Text,
CultureInfo.CurrentCulture).ToString("000000")
This is what the framework does for you internally anyway when you use the
version with only one parameter

/claes
 
Back
Top