473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

checking for data type

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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
6 5125
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

Mike P wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #2
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,

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

"Mike P" <mi*******@gmail.com> wrote in message
news:O4**************@TK2MSFTNGP12.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #3
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.

"Mike P" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #4
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uJ**************@TK2MSFTNGP14.phx.gbl...
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
Nov 21 '05 #5
"Mrinal Kamboj" <mr***********@oracle.com> wrote in message
news:Ez**************@news.oracle.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

Nov 21 '05 #6
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" <ja*********@mvps.org> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:uJ**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
6
by: Web Developer | last post by:
Hi, I come across the term "type checking" very often in my readings on C++, and have never heard it in Java. Besides the simplistic answer that it checks the "type", what more does it mean? ...
2
by: mike | last post by:
I had a form like below that validated that a file was there before it would submit. <form name="attach" method="POST" action="run_this_pgm.cfm" enctype="multipart/form-data"...
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
8
by: Brendan | last post by:
There must be an easy way to do this: For classes that contain very simple data tables, I like to do something like this: class Things(Object): def __init__(self, x, y, z): #assert that x,...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
3
by: w.m.gardella.sambeth | last post by:
Hi all, I am more or less new to Python, and currently am making my first "serious" program. The application is a Clinical History manager (for my wife) which stores its data on a sqlite database....
1
by: ledneh | last post by:
I've been working on concurrency checking for an application I'm building, and a minor part of it has me slightly stumped. I've got a DetailsView that populates from an ObjectDataSource, using...
16
by: Joe Strout | last post by:
Let me preface this by saying that I think I "get" the concept of duck- typing. However, I still want to sprinkle my code with assertions that, for example, my parameters are what they're...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.