Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

conerting string to long

Posted on 2011-02-22
2
Medium Priority
?
564 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 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