Solved

conerting string to long

Posted on 2011-02-22
2
554 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

739 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