Results 1 to 4 of 4

Thread: [RESOLVED] Please help with : Conversion from string "Label1" to type 'Double' is not valid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    234

    Resolved [RESOLVED] Please help with : Conversion from string "Label1" to type 'Double' is not valid

    Hi everyone, I need help with problem (Conversion from string "Label1" to type 'Double' is not valid)

    Here is my code:
    Public Class Form1


    Public Function getDistance(ByVal latA As Double, ByVal longA As Double, ByVal latB As Double, ByVal longB As Double, ByVal distanceUnit As Char) As Double

    Dim distCalculated As Double
    Dim distTheta As Double

    distTheta = longA - longB
    distCalculated = Math.Sin(dgToRad(latA)) * Math.Sin(dgToRad(latB)) + Math.Cos(dgToRad(latA)) * Math.Cos(dgToRad(latB)) * Math.Cos(dgToRad(distTheta))
    distCalculated = Math.Acos(distCalculated)
    distCalculated = radToDg(distCalculated)
    distCalculated = (distCalculated * 60) * 1.1515
    distCalculated = distCalculated * 0.868976242

    Return distCalculated
    End Function
    Private Function dgToRad(ByVal dg As Double) As Double
    Return (dg * Math.PI / 180.0)
    End Function
    Private Function radToDg(ByVal rad As Double) As Double
    Return rad / Math.PI * 180.0
    End Function

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Dim myanswer As Double
    myanswer = Math.Round(getDistance(Me.LatADECLabel1.Text, Me.LonADECLabel1.Text, Me.LatBDecLabel1.Text, Me.LonBDecLabel1.Text, "M"))
    Label2.Text = myanswer
    End Sub

    End Class

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,470

    Re: Please help with : Conversion from string "Label1" to type 'Double' is not valid

    I would think that the error message would speak for itself. Would you expect that the String "Label1" would be able to be converted to a Double value? Presumably you are clicking Button2 without ever having changed the Text of one of at least one of those Labels from its default.

    Why are you converting the Text of Labels to numbers in the first place? The user can't set the Text of a Label so you must be doing it code, which means that you must already have that data. You should be setting the Text of the Label from a Double variable that you already have so, when it comes time to perform the calculation, use that Double variable. Labels are not a place to store data for later use. That's what variables are for. The Label's purpose is solely to display text to the user. If you want to store numbers for calculations then do exactly that.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    234

    Re: Please help with : Conversion from string "Label1" to type 'Double' is not valid

    Quote Originally Posted by jmcilhinney View Post
    I would think that the error message would speak for itself. Would you expect that the String "Label1" would be able to be converted to a Double value? Presumably you are clicking Button2 without ever having changed the Text of one of at least one of those Labels from its default.

    Why are you converting the Text of Labels to numbers in the first place? The user can't set the Text of a Label so you must be doing it code, which means that you must already have that data. You should be setting the Text of the Label from a Double variable that you already have so, when it comes time to perform the calculation, use that Double variable. Labels are not a place to store data for later use. That's what variables are for. The Label's purpose is solely to display text to the user. If you want to store numbers for calculations then do exactly that.
    Thank you I must have had a blonde day, went back on some old code that I wrote a few years back and tried to modify it for another purpose. Got it working now, many thanks :/

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,470

    Re: [RESOLVED] Please help with : Conversion from string "Label1" to type 'Double' is

    All's well what ends well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width