Solved

Convert string to long

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
Calling stored proc in EDMX 11 23
How can I set a timer to automatically take a picture using the Xamarin MediaPlugin 2 45
C# parent child form 5 13
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
A short film showing how OnPage and Connectwise integration works.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 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

11 Experts available now in Live!

Get 1:1 Help Now