?
Solved

Convert string to long

Posted on 2006-06-20
5
Medium Priority
?
259 Views
Last Modified: 2010-04-16
How to do this. The string may not always hold a long value?
0
Comment
Question by:XPUSR
[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
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:vo1d
ID: 16944825
which framework do you use?
0
 
LVL 4

Expert Comment

by:fffej78
ID: 16944851
String s = "123"
long x = long.parse( s ); // or Long if you are using .NET v1

It throws a FormatException is the string isn't in the correct format

try
{
  String s = "WRONG!";
  return long.parse( s );
}
catch ( FormatException ex )
{
  return -1; // or whatever you want to do to indicate a fault.
}

0
 
LVL 11

Expert Comment

by:vo1d
ID: 16944854
if you are using net 2, you can use this teh long.TryParse method
example:

static void Main(string[] args)
        {            
            string test = "321311231";
            long val;
            if(long.TryParse(test, out val))
            {
                MessageBox.Show("ok");
            }
            else
            {
                MessageBox.Show("failed");
            }          
        }              
0
 
LVL 4

Accepted Solution

by:
fffej78 earned 500 total points
ID: 16944869
Sweet, didn't know about that method.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16944889
yeha, net 2 has some nice new statics.
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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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