Solved

Convert string to long

Posted on 2006-06-20
5
250 Views
Last Modified: 2010-04-16
How to do this. The string may not always hold a long value?
0
Comment
Question by:XPUSR
  • 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 125 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

20 Experts available now in Live!

Get 1:1 Help Now