Vlad Stef
Vlad Stef

Reputation: 13

How to use a Sub value in a second Sub

I am new to VB.net and really need bigger brains for this: (all code added in one module)

I have a random function (that is giving me a random text value). This is how I call the random function:

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

Now, I have a Sub that takes the Random text value and displays that in a Richtextbox, with a typewriter effect:

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

I also have a Thread that I want to call in Sub Type()

Private Sub voice()
    Dim TheSpeaker As New Speech.Synthesis.SpeechSynthesizer()
    TheSpeaker.SelectVoiceByHints(Synthesis.VoiceGender.Female)
    TheSpeaker.Speak(RandomIpAddress)
End Sub

My problem is: how to get the RandomIpAddress in both Sub type() and Private Sub voice?

If I'm using the:

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

inside the Module, then my code is running ONCE correctly. After that the Random code is not working any more (it's loading the same text) - never changing the result (no random).

If I am going to move the "Dim" code inside SUB and Thread, then I will have a random text added in richtextbox and another in Thread. So is doing a random result for both. I just want to get the same random result in both!

Here is my full code:

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

Upvotes: 0

Views: 92

Answers (1)

Abr001am
Abr001am

Reputation: 591

It seems either you are not getting your task clear or not getting us clear about it.

There is a handful of remarks to pay for this strip of code

  1. Your variable max isn't used in your function GetRandom, that is not a foretelling sign in favor of same function's results. I believe you are missing Return rdm.Next(19, max) somewhere in your GetRandom(max) function, just a prediction that has a strong likelihood to be applicable

  2. RandomIpAddress is declared a static variable, while you are using it as a function.

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

    Thus the use of it would differ to:

    Form1.str_ = RandomIpAddress()
    

    And

    Dim cleanString As String = Replace(RandomIpAddress(), ".", " ")
    
  3. Threads are independant entities, they dont have access to other forms' ressources unless you share them.

    Declaration of your textbox must be:

    Friend Shared WithEvents TextBox1 As TextBox .
    

Upvotes: 1

Related Questions