Invoking a function with a click event in form (VB) RRS feed

  • Question

  • I am building a form to convert a number score into a letter grade and need to invoke a function that is called when I click on the "btnCalc" button. The input field on the form is titled "txtScore" and the output field is "txtGrade".

    The code below isn't working and I'm also getting this error: " 'txtScore' is ambiguous because multiple kinds of members with this name exist in class"

    I'm not sure what could be wrong, I've gone through a lot tutorials and documentation but can't seem to resolve. Would someone be able to help?

    Here is my code:

    Public Class GradeForma
    Dim txtScore = Math.Round(txtScore)
    Public Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
    End Sub
    Public Function GetLetterGrade(ByVal dblGrade As Double) As String
        If txtScore <= 59 Then
            txtGrade.Text = "F"
        ElseIf txtScore <= 69 Then
            txtGrade.Text = "D"
        ElseIf txtScore <= 79 Then
            txtGrade.Text = "C"
        ElseIf txtScore <= 89 Then
            txtGrade.Text = "B"
        ElseIf txtScore <= 100 Then
            txtGrade.Text = "A"
        End If
    End Function

    Monday, October 30, 2017 11:04 PM

All replies

  • In:

    Dim txtScore = Math.Round(txtScore)

    You are declaring txtScore but you are also using it. That won't work. I assume that elsewhere you have something else called txtScore, so you probably must use a different name in the "Dim" statement. The name "txtScore" implies a string (text) but since you are using Math.Round I assume that you want a number (probably a double).

    My guess is that txtScore is a text control and if so then you need to specify a property, probably either txtScore.Text or txtScore.Value; my old-timer brain does not always remember details so I am not sure.

    Then you call GetLetterGrade(txtScore) but the argument for GetLetterGrade is called dblGrade so within the GetLetterGrade  subroutine you should use dblGrade, not txtScore. Since you are passing a double to the subroutine I assume that the Dim should be creating a double.

    Sam Hobbs

    • Edited by Simple Samples Monday, October 30, 2017 11:40 PM
    • Proposed as answer by IronRazerz Monday, October 30, 2017 11:48 PM
    Monday, October 30, 2017 11:38 PM
  •  That would imply that you probably have a control on the form that has the same name as your variable that is declared at the top of your posted code,  txtScore.


    If you say it can`t be done then i`ll try it

    Monday, October 30, 2017 11:46 PM
  • Hi

    As others have mentioned, there are issues with your code. Here is an alternative approach. This example needs a TextBox called txtScore (which I think you already have) and a Label called txtGrade (maybe you have this too)

    ' Form1 with TextBox named 'txtScore'
    ' Label called 'txtGrade' for Letter Grade display

    ' These Options help to avoid some of the common
    ' errors as you enter code into the editor
    Option Strict On
    Option Explicit On

    ' Here,I am using Form1, where you are using 'GradeForma'
    ' so, if you want to try this out, do not copy ALL this
    ' code into your editor, just copy everything EXCEPT the
    ' Public Class GradeForma   line.
    Public Class Form1
        Public Function GetLetterGrade(ByVal dblGrade As String) As String
            Select Case GetDouble(dblGrade)
                Case <= 59
                    Return "F"
                Case <= 69
                    Return "D"
                Case <= 79
                    Return "C"
                Case <= 89
                    Return "B"
                Case Else
                    Return "A"
            End Select
        End Function

        ' this is a Function to change a string representation
        ' of a Double into an actual Double value
        Function GetDouble(s As String) As Double
            Dim v As Double = 0.0
            If Double.TryParse(s, v) Then Return v
            Return 0.0
        End Function

        ' this is called every time a change is made to the
        ' text in TextBox (called txtScore) and updates
        ' the Label txtGrade text with the Letter Grade
        Private Sub txtScore_TextChanged(sender As Object, e As EventArgs) Handles txtScore.TextChanged
            txtGrade.Text = GetLetterGrade(txtScore.Text)
        End Sub
    End Class

    Regards Les, Livingston, Scotland

    Tuesday, October 31, 2017 12:12 AM
  • Hi ndotlara,

    There are two error that you may need to modify, one is txtscore, it may be one Textbox control, and you want to convert this value into double, you can assign this value to the other field. Another is function, you don't need to use function, because you don't return a value.

    I modify you code and you can take a look:

     Dim score As Double
        Private Sub txtScore_TextChanged(sender As Object, e As EventArgs) Handles txtScore.TextChanged
            If Double.TryParse(txtScore.Text, score) = True Then
                MessageBox.Show("Please enter the correct score!")
            End If
        End Sub
        Public Sub GetLetterGrade(ByVal dblGrade As Double)
            If dblGrade <= 59 Then
                txtGrade.Text = "F"
            ElseIf dblGrade <= 69 Then
                txtGrade.Text = "D"
            ElseIf dblGrade <= 79 Then
                txtGrade.Text = "C"
            ElseIf dblGrade <= 89 Then
                txtGrade.Text = "B"
            ElseIf dblGrade <= 100 Then
                txtGrade.Text = "A"
            End If
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        End Sub

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, October 31, 2017 6:22 AM