473,403 Members | 2,183 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,403 software developers and data experts.

check, if string is convertable to another type

Hello,

This is maybe a simple one:
Is there any way to check, if a string can be converted to
another type (like DateTime or Integer) without using
try/catch? In VB, i can use IsDate or IsNumeric to do
this, but how can i do it in C#?

Thanks in advance,
Daniel
Nov 15 '05 #1
2 7131

"Daniel" <an*******@discussions.microsoft.com> schreef in bericht
news:08****************************@phx.gbl...
Hello,

This is maybe a simple one:
Is there any way to check, if a string can be converted to
another type (like DateTime or Integer) without using
try/catch? In VB, i can use IsDate or IsNumeric to do
this, but how can i do it in C#?

Thanks in advance,
Daniel


There is no such thing as IsNumeric in C#. You'll have to work with a try
catch to test it or use the VB function.

public static bool IsNumeric(string input)
{
try
{
int.Parse(input);
return true;
}
catch
{
return false;
}
}

Yves
Nov 15 '05 #2
Decimal has a TryParse function that won't throw an exception.

C# can reference the VB assemblies and call IsNumeric (shudder) -- please
don't do this. It's been a while since I looked at the code for IsNumeric,
but I think, in addition to doing some additional processing (VB is
sometimes more lenient on conversions), it might do a Try/Catch internally.

Try/Catch is usually how it's done, unless it's a performance critical
section, in which case you might run it through a regex first (remember to
take into account culture settings).

-mike
MVP

"Daniel" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
Hello,

This is maybe a simple one:
Is there any way to check, if a string can be converted to
another type (like DateTime or Integer) without using
try/catch? In VB, i can use IsDate or IsNumeric to do
this, but how can i do it in C#?

Thanks in advance,
Daniel

Nov 15 '05 #3

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

Similar topics

27
by: John Roth | last post by:
PEP 263 is marked finished in the PEP index, however I haven't seen the specified Phase 2 in the list of changes for 2.4 which is when I expected it. Did phase 2 get cancelled, or is it just not...
0
by: chris | last post by:
Hello, I have been reading the c++ standard recently, in particular the part about input iterators. I note that given an input iterator a for type T, then *a only has to be "convertable to T",...
9
by: Ronald Fischer | last post by:
Assume the following JavaScript function: function bracketize(s) { return ''; } This function which doesn't assume anything about its argument except that it must be convertible to a string.
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
2
by: Daniel | last post by:
Hello, maybe this is an easy one: Is there any way to check, if a string value can be converted into another type (e.g. DateTime, Integer) without using exceptions (try/catch)? Thanks in...
4
by: Gaijinco | last post by:
I made this function which given two strings c1 and c2 test if with the letters of both you can exactly form the string c3, so for exaple c1 = "evol" c2= "ution" and c3 = "evolution" will yield...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
2
by: rn5a | last post by:
A sub-routine retrieves the value in a TextBox (which ought to be an integer) which in turn resides in a DataGrid (actually the column is a BoundColumn which changes to a TextBox when the DataGrid...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.