如何在第二个Sub中使用Sub值

时间:2017-06-25 12:44:18

标签: vb.net function

我是VB.net的新手,真的需要更大的脑筋: (所有代码都添加在一个模块中)

我有一个随机函数(即给我一个随机文本值)。 这就是我调用随机函数的方法:

Dim IpAddresses As String() = My.Resources.BrainAnswer.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Dim RandomIpAddress As String = IpAddresses(GetRandom(IpAddresses.Length))

现在,我有一个Sub,它接受Random文本值并在Richtextbox中显示,具有打字机效果:

Sub type()
    Dim thread As New Thread(AddressOf voice)
    thread.Start()
    Form1.RichTextBox1.Refresh()
    Form1.count_ = 1
    Form1.RichTextBox1.Text = Form1.str_
    Form1.RichTextBox1.Clear()
    Form1.str_ = RandomIpAddress
    Form1.Timer1.Enabled = True
End Sub

我还想在Sub Type()

中调用一个Thread
Private Sub voice()
    Dim TheSpeaker As New Speech.Synthesis.SpeechSynthesizer()
    TheSpeaker.SelectVoiceByHints(Synthesis.VoiceGender.Female)
    TheSpeaker.Speak(RandomIpAddress)
End Sub

我的问题是:如何获得RandomIpAddressSub type()中的Private Sub voice

如果我使用的是:

Dim IpAddresses As String() = My.Resources.BrainAnswer.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Dim RandomIpAddress As String = IpAddresses(GetRandom(IpAddresses.Length))

在模块内部,然后我的代码正确运行ONCE。之后,随机代码不再工作(它正在加载相同的文本) - 永远不会改变结果(不随机)。

如果我要在SUB和Thread中移动“Dim”代码,那么我将在richtextbox中添加一个随机文本,在Thread中添加另一个文本。所以两者都是随机的结果。我只想在两者中获得相同的随机结果!

这是我的完整代码:

Imports System.Threading
Imports System.Speech
Imports System.Speech.Recognition

Module brain

    Public str_ As String
    Private rdm As New Random

    Private Function GetRandom(max As Integer) As Integer

        If InStr(UCase(Form1.TextBox1.Text), "HELLO MOTHER") Then
            Dim theTime As DateTime
            theTime = Now.ToLongTimeString
            If theTime >= #6:00:00 AM# AndAlso theTime <= #9:59:59 AM# Then
                Return rdm.Next(0, 3)
            Else
                Return rdm.Next(3, 6)
            End If
        End If

        If InStr(UCase(Form1.TextBox1.Text), "HOW ARE YOU") Then
            Return rdm.Next(6, 8)
        End If

        If InStr(UCase(Form1.TextBox1.Text), "WHO ARE YOU") Then
            Return rdm.Next(8, 11)
        End If

        If InStr(UCase(Form1.TextBox1.Text), "YOUR NAME") Then
            Return rdm.Next(11, 13)
        End If

        If InStr(UCase(Form1.TextBox1.Text), "WHAT ARE YOU") Then
            Return rdm.Next(13, 16)
        End If

        If InStr(UCase(Form1.TextBox1.Text), "WHAT DO YOU DO") Then
            Return rdm.Next(16, 18)
        End If

        If InStr(UCase(Form1.TextBox1.Text), "WHAT CAN YOU DO") Then
            Return rdm.Next(16, 18)
        End If

        If InStr(UCase(Form1.TextBox1.Text), "HELP ME") Then
            Return rdm.Next(16, 18)
        End If

        If InStr(UCase(Form1.TextBox1.Text), "YOUR MISSION") Then
            Return rdm.Next(18, 19)
        End If

    End Function

    Dim IpAddresses As String() = My.Resources.BrainAnswer.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    Dim RandomIpAddress As String = IpAddresses(GetRandom(IpAddresses.Length))


    Sub type()
        Dim thread As New Thread(AddressOf voice)
        thread.Start()
        Form1.RichTextBox1.Refresh()
        Form1.count_ = 1
        Form1.RichTextBox1.Text = Form1.str_
        Form1.RichTextBox1.Clear()
        Form1.str_ = RandomIpAddress
        Form1.Timer1.Enabled = True
    End Sub


    Private Sub voice()
        Dim TheSpeaker As New Speech.Synthesis.SpeechSynthesizer()
        TheSpeaker.SelectVoiceByHints(Synthesis.VoiceGender.Female)
        Dim cleanString As String = Replace(RandomIpAddress, ".", " ")
        TheSpeaker.Speak(cleanString)

    End Sub


End Module

1 个答案:

答案 0 :(得分:1)

看来要么你没有明确你的任务,要么没有让我们明白它。

这段代码需要付出一些评论

  1. 您的变量max未在您的函数GetRandom中使用,这不是支持相同函数结果的预示符号。 我相信您在Return rdm.Next(19, max)函数的某个地方遗失了GetRandom(max),只是一个很有可能适用的预测

  2. RandomIpAddress被声明为静态变量,而您将其用作函数。

    Public Delegate Function newfunction() as string
    Public RandomIpAddress As newfunction = Function() IpAddresses(GetRandom(IpAddresses.Length))
    

    因此,它的使用将不同于:

    Form1.str_ = RandomIpAddress()
    

    Dim cleanString As String = Replace(RandomIpAddress(), ".", " ")
    
  3. 主题是独立的实体,他们无权访问其他形式的&#39;除非你分享它们,否则需要资源。

    您的文本框声明必须是:

    Friend Shared WithEvents TextBox1 As TextBox .