Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

Convert string to long

How to do this. The string may not always hold a long value?
0
XPUSR
Asked:
XPUSR
  • 3
  • 2
1 Solution
 
vo1dCommented:
which framework do you use?
0
 
fffej78Commented:
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
 
vo1dCommented:
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
 
fffej78Commented:
Sweet, didn't know about that method.
0
 
vo1dCommented:
yeha, net 2 has some nice new statics.
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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now