We help IT Professionals succeed at work.

Visual Studio VB 2005 Convert String to Single and other types

Murray Brown
Murray Brown asked
on
Medium Priority
1,038 Views
Last Modified: 2007-12-19
Hi

I have to convert a string Variable to Single. How does one do this.

Also how do you convert string values to other common types?

Thanks very much

Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
       Dim s As String = "1.444"
        Dim sng As Single

        Try
            sng = CType(s, Single)
        Catch ex As InvalidCastException
            MessageBox.Show("Invalid conversion")
        End Try

Bear in mind that an "InvalidCastException" will be thrown if the cast you are trying is not possible.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Commented:

        TryParse is a new feature in Vb2005

        Dim value As Integer = 0
        If Integer.TryParse("12", value) = False Then
            MessageBox.Show("invalid")
        End If
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.