none
Picking Random Quote to put into Label RRS feed

  • Question

  • I'm new to programming (in high school) and need some help on this. Yes, this is a school assignment, but I'm not just looking for a copy and paste answer like most (I know you guys must hear this alot).

    I'm trying to make the label show an insult every time I press the button.

    This is the code I have so far, but it gives me an error every time I try. Can someone explain why this doesn't work? Is my syntax just off?

    Public Class Form1

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim insult1 As String
            Dim insult2 As String
            Dim insult3 As String
            insult1 = "Lets meet as little as we can"
            insult2 = "I desire that we be better strangers"
            insult3 = "Boys, apes, braggarts, jacks, milksops"
            Label1.Text = insult1 or insult2 or insult3
        End Sub
    End Class

    Tuesday, December 11, 2018 5:42 PM

All replies

  • Hi

    Good try but slightly incorrect.

    My suggestions:

    *

    In the Form Load event.

    Build a List(Of String) with all the Insults.

    First declare the List - example Dim Insults as New List(Of String)

    Secondly, add the strings - example Insults.Add("Lets meet as little as we can") - repeat for each Insult.

    *

    Declare a random (Integer) at Class level.

    Example:

    Dim rand As New Random Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' etc ..............

    *

    In your Button click event.

    Choose a new random number within the range of 0 (zero) to the List size. Example Dim r as Integer = rand.Next(0,Insults.Count)

    Get the random item from the list:

    Dim s as String = Insults(r)

    *

    Put found Insult in Label:

    Label1.Text = s


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, December 11, 2018 5:58 PM
    Tuesday, December 11, 2018 5:56 PM
  • OK, I tried what you suggested, but got a few errors. Not sure what I messed up on - I've never worked with the lists or anything before. What looks wrong here?

    Public Class Form1
        Dim rand As New Random
        Dim Insults As New List(Of String)
        Insults.Add("you suck")
        Insults.Add("you are dumb")
        Insults.Add("insult")
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim r As Integer = Random.Next(0, Insults.Count)
            Dim s As String = Insults(r)
            Label1.Text = 5
        End Sub
    End Class

    I get a "declaration expected" error on line 5,6, and 7. I also got a "reference to non-shared member requires an object reference" on line 9, but I'm not sure what that means.

    Tuesday, December 11, 2018 6:38 PM
  • Hi

    Thiose errors were inevitable as you put everything at the Class level and not in Sub's.

    In general, you only declare variables at Class level and use them in Subs/Functions.

    You do not need to declare *every* variable at Class level, only those that are used in several different Subs/Functions.

    To get the code you posted was very straightforward, just needed to put into Subs and alter one or 2 minor errors.

    ' I suggest these 2 lines. They will
    ' assist in preventing some of
    ' the common code errors.
    Option Strict On
    Option Explicit On
    
    Public Class Form1
      ' these 2 lines are declared at Class
      ' level and so are 'seen' throughout
      ' the whole of this Class
      Dim rand As New Random
      Dim Insults As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' here, initialize and fill the
        ' List with the Insults
        Insults.Add("you suck")
        Insults.Add("you are dumb")
        Insults.Add("insult")
      End Sub
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' NOTE: we are using the Random 
        ' which was declared at Class level
        ' and not the Random that you used.
        Dim r As Integer = rand.Next(0, Insults.Count)
        Dim s As String = Insults(r)
        ' here we put the String 's' into
        ' the Text property of the Label1
        ' and not the literal '5' you used.
        Label1.Text = s
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Proposed as answer by dbasnett Tuesday, December 11, 2018 7:37 PM
    Tuesday, December 11, 2018 6:52 PM
  • Why stop at three ?

    Public Class Form1
        Private _items As New List(Of String)() From
            {"Some value", "Hello", "Apples", "Coffee", "Last item", "Pie"}
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim rnd As New Random()
            Label1.Text = _items.OrderBy(Function(item) rnd.Next()).FirstOrDefault()
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, December 11, 2018 9:39 PM
    Moderator
  • A very simple solution, using the New Random() function and a Select Case block.  Begin with 0 for the first case.  You can add as many cases as you'd like, and change the number after Next().

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim rnd As New Random()
            Dim insult As Integer
            insult = rnd.Next(3)
            Select Case insult
                Case 0
                    TextBox1.Text = "bad"
                Case 1
                    TextBox1.Text = "worse"
                Case 2
                    TextBox1.Text = "terrible"
            End Select
        End Sub


    Solitaire

    Thursday, December 13, 2018 7:02 PM