473,769 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7146

"Daniel" <an*******@disc ussions.microso ft.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(strin g 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*******@disc ussions.microso ft.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
2606
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 in the changes document? John Roth
0
1340
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", not actually of type T. Firstly, I can't seem to find an exact definition of "convertable to T". While I assume it is the obvious definition (an implicit conversion), I can't find an exact definition. Is there one?
9
1846
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
2129
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
2466
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 advance, Daniel
4
2173
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 true. The function looks like this: #include <iostream> #include <algorithm> #include <string> using namespace std;
6
2206
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 and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
8
1934
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 the array m_columnBlowStep is string because I have used ToString on each enum item as you can see. I want to use enum in the case statment in the switch. I know I can use string but I rather want to use enum.
2
719
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 is in the editable mode). This is the code: ----------------------------------- Dim strQty As String Dim iQty As Integer strQty = CType(ea.Item.Cells(5).Controls(0), TextBox).Text
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7408
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.