checking for data type

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I'm using this code to check if a user input is of a particular data
type :

try
{
int intUserInput = Convert.ToInt32 (txtUserInput.Text);
}
catch
{
//invalid user input
}


Is there a smarter C# way of doing this?


Thanks,

Mike
 
Hi ,

Few ways that i can think of are :

1. You can go for int.parse , instead of Convert.Toint32 , i think
that's more useful , but don't have relevant data , however it will
again go through Exception block for incorrect input , since there's no
inherent conversion / compatibility when converting from string to int .

another way is to use Regex and in that case you will avoid Exception
block altogether -- something like :

Regex r = new Regex("(\d)+") ;

regards ,

Mrinal
 
Hi,

If this is the only use you will have, it would be much better to use
TextBox.KeyDown/KeyPress event, there you could check if the key is a number
and avoid entering incorrect data from the beginning.


cheers,
 
If you can use the .NET 2.0 framework, they've added a "TryParse" function
that you can use to see if a string can be parsed into a particular data type
without causing an exception to be thrown.

Brian.
 
If this is the only use you will have, it would be much better to use
TextBox.KeyDown/KeyPress event, there you could check if the key is a number
and avoid entering incorrect data from the beginning.

That sounds like far more effort than it's worth. (e.g., it's not just
digits -- you have to remember to allow backspace also). And then we have
to worry about range. Is "9999999999999" OK? What about "0000000000001" ?

As sloppy as the try/catch/Convert paradigm is, it's the better we've
got until we've all converted to v2.0 & TryParse.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
1. You can go for int.parse , instead of Convert.Toint32 , i think
that's more useful , but don't have relevant data , however it will
again go through Exception block for incorrect input , since there's no
inherent conversion / compatibility when converting from string to int .

Actually, all Convert.ToInt32 does is call int.Parse, so it's pretty
much a wash.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Hi,

It's very easy , especially if you can find it in the archives and just
have to copy it :)

The good thing about it is that you prevent the user from entering ilegal
chars, IMO it's safer, you can ALWAYS use tb.Text knowing it does contains
a valid number.
It also is easier for the user to realize it needs to enter a number.


Another possibility is derive from TextBox and override CreateParams:

const int ES_NUMBER = 0x2000;


protected override System.Windows.Forms.CreateParams CreateParams
{
get
{
System.Windows.Forms.CreateParams cp = base.CreateParams;
cp.Style |= ES_NUMBER;


return cp;
}



}


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



James Curran said:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message
If this is the only use you will have, it would be much better to use
TextBox.KeyDown/KeyPress event, there you could check if the key is a number
and avoid entering incorrect data from the beginning.

That sounds like far more effort than it's worth. (e.g., it's not just
digits -- you have to remember to allow backspace also). And then we have
to worry about range. Is "9999999999999" OK? What about "0000000000001"
?

As sloppy as the try/catch/Convert paradigm is, it's the better we've
got until we've all converted to v2.0 & TryParse.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Back
Top