Solved

conerting string to long

Posted on 2011-02-22
2
557 Views
Last Modified: 2012-05-11
for the following piece of code:
long Priority = Convert.ToInt64(IsPdto.Priority);

I want to make sure that I do not get a runtime error, in case IsPdto.Priority is not a number in string format. How can I make sure that I do not perform this operation if it is not convertible to int?
0
Comment
Question by:vivekj2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
Misbah earned 500 total points
ID: 34955842
you need to use long.TryParse method


long Priority = 0;
bool isCorrect = long.TryParse(IsPdto.Priority, out Priority);
if(isCorrect)
{
// 
}

Open in new window

0
 

Expert Comment

by:sshull
ID: 34955879
If you want, you could put it in a try catch and ignore the error. For example:

try
{
long Priority = Convert.ToInt64(IsPdto.Priority);
}
catch
{
}

Since the error is handled by the code (even though it doesn't do anything), a runtime error won't occur. If you want you can even generate your own error message in your catch statement if it's a field that must be a valid numeric or you can set Priority to 0 if the field doesn't matter.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question