Solved

Convert string to long

Posted on 2006-06-20
5
257 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
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

738 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