VB从另一个函数调用公共函数

时间:2015-10-09 11:56:34

标签: vb.net call

如何从另一个函数调用公共函数?这是我的VB石头剪刀项目中的语法错误。 (我在VBA中调用了其他公共函数的公共函数,但这在VB中显然是不同的。)

Public Class Form1
Public playerchoice, strexplanation, strwinner, computerchoice

Public Sub computerchoice()
    Dim randomgenerator As New Random
    Dim intcomputerchoice As Integer = randomgenerator.Next(1, 4) ' number from 1-3
    If intcomputerchoice = 1 Then intcomputerchoice = "rock"
        Else if intcomputerchoice = 2 Then intcomputerchoice = "paper"
        ElseIf intcomputerchoice=3 Then intcomputerchoice="scissors"
    End If
End Sub
Public Sub winner()
    If playerchoice = rock Then
        If computerchoice = rock Then
            strexplanation = "Tie"
        ElseIf computerchoice = paper Then
            strexplanation = "Computer wins. Paper covers rock."
        ElseIf computerchoice = scissors Then
            strexplanation = "Player wins. Rock crushes scissors."
        End If
    End If
    If playerchoice = paper Then
        If computerchoice = paper Then
            strexplanation = "Tie"
        ElseIf computerchoice = scissors Then
            strexplanation = "Computer wins. Paper cuts scissors."
        ElseIf computerchoice = rock Then
            strexplanation = "Player wins. Paper covers rock."
        End If
    End If
    If playerchoice = scissors Then
        If computerchoice = scissors Then
            strexplanation = "Tie"
        ElseIf computerchoice = rock Then
            strexplanation = "Computer wins. Rock crushes paper."
        ElseIf computerchoice = paper Then
            strexplanation="Player wins. Scissors cut paper."S
        End If
    End If
End Sub

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
    Me.Close()
End Sub

Public Sub rock_Click(sender As Object, e As EventArgs) Handles rock.Click
    Call Function computerchoice
    playerChoice = rock
    Call Function winner
End Sub

Public Sub paper_Click(sender As Object, e As EventArgs) Handles paper.Click
    Call function computerchoice()
    playerChoice = paper
    Call Function winner
End Sub

Public Sub scissors_Click(sender As Object, e As EventArgs) Handles scissors.Click
    Call Function computerchoice
    playerChoice = scissors
    Call Function winner
End Sub

End Class

代码的麻烦部分:

Public Sub rock_Click(sender As Object, e As EventArgs) Handles rock.Click
    Call Function computerchoice
    playerChoice = rock
    Call Function winner
End Sub

我在VS Express 2012的“呼叫功能”行周围出现蓝线表示错误。

1 个答案:

答案 0 :(得分:0)

对于您的特定方案,您可以更改方法签名 以下内容:

Public Sub computerchoice()
    Dim randomgenerator As New Random
    Dim intcomputerchoice As Integer = randomgenerator.Next(1, 4) ' number from 1-3
    If intcomputerchoice = 1 Then intcomputerchoice = "rock"
        Else if intcomputerchoice = 2 Then intcomputerchoice = "paper"
        ElseIf intcomputerchoice=3 Then intcomputerchoice="scissors"
    End If
   winner() 'Calling winner function inside computerchoice
End Sub

因此,按钮点击将如下所示:

Public Sub rock_Click(sender As Object, e As EventArgs) Handles rock.Click
   computerchoice()   
End Sub