Solved

conerting string to long

Posted on 2011-02-22
2
513 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
2 Comments
 
LVL 9

Accepted Solution

by:
Misbah earned 500 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now