random number selection and display

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Modibbo
    New Member
    • Apr 2007
    • 33

    random number selection and display

    Hi there,

    I'd like to submit this program I'm working on to thr forum.
    I want to display 6 randomly generated numbers on 6 command buttons as captions at the click of another button. One number will be selected only once.
    Here's my code:
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x As Integer() = New Integer() {x(0), x(1), x(2), x(3), x(4), x(5)}
            Dim m_blnUsed As Boolean() = _
            New Boolean(x.GetUpperBound(0)) {}
    
            Dim objrandom As Random = New Random()
            Dim intrandom As Integer
            Do
                intrandom = objrandom.Next(0, m_blnUsed.Length)
            Loop Until m_blnUsed(intrandom) = False
            m_blnUsed(intrandom) = True
    
            Dim index As Integer
            For index = 0 To 5
                x(index) = x(intrandom)
            Next
    
            Button3.Text = Str(x(0))
            Button4.Text = Str(x(1))
            Button5.Text = Str(x(2))
            Button6.Text = Str(x(3))
            Button7.Text = Str(x(4))
            Button8.Text = Str(x(5))
    
        End Sub
        
    End Class
    I'm getting an error message
    variable 'x' is used before it has been assigned a value. A null reference exception could result at runtime.
    at the beginning where I'm declaring and initializing the array of integers x(0)

    Can anyone help please.
    Thanks
  • Modibbo
    New Member
    • Apr 2007
    • 33

    #2
    forgot to mention I'm using VB2005

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #3
      Originally posted by Modibbo
      Dim x As Integer() = New Integer() {x(0), x(1), x(2), x(3), x(4), x(5)}
      Think what you are telling VB to do:
      "I want to declare an array of integers called x, and initialize it to the values in X(0), X(1), ..."
      So, VB is saying, duh, you don't have anything in x, so I cannot use it to initialize anything, much less itself.

      What do you really want to initialize x to?

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Also, I think there's a loop missing there. Unless I'm misreading the code, intrandom is only set once, so all six occurences of x (and hence the buttons) will be set to the same number.

        Comment

        • SammyB
          Recognized Expert Contributor
          • Mar 2007
          • 807

          #5
          Originally posted by Killer42
          Also, I think there's a loop missing there. Unless I'm misreading the code, intrandom is only set once, so all six occurences of x (and hence the buttons) will be set to the same number.
          Oh, no! This is the same thread as http://www.thescripts.com/forum/thread636691.html with the same mistakes!

          OK. I will write an article on random number generation and post it in our new Articles Tab. I'll post a link here afterwards.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Beat you to it, Sammy. :D

            Here is the link.

            Comment

            • Modibbo
              New Member
              • Apr 2007
              • 33

              #7
              thanks a lot sammyB, killer42 for all your help; i've been going trough all the material you've suggested, even some of it i read before by searching, but still can't get my head round it. nevertheless still trying, here's the modified code
              Code:
              Public Class Form1
              
              
                  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                      Dim x As Integer() = New Integer(6) {}
              
                      Dim randomgeberator As New Random
                      Dim index As Integer
                      For index = 0 To 5
                          x(index) = randomgeberator.Next(0, 20)
                      Next
              
                      Button3.Text = Str(x(0))
              
              
                      Button4.Text = Str(x(1))
                      Button5.Text = Str(x(2))
                      Button6.Text = Str(x(3))
                      Button7.Text = Str(x(4))
                      Button8.Text = Str(x(5))
              
                  End Sub
              
              End Class
              i'm having this at runtime on the immediate window
              A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
              A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
              A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
              A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
              A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
              now i'm being able to select randomly 6 numbers and display them in the buttons, the bit i can't achieve is to select one number once only which i was trying to do with the boolean array but i'm having trouble to set it up
              any help will be so much appreciated
              thanks

              Comment

              • Modibbo
                New Member
                • Apr 2007
                • 33

                #8
                I've been able to achieve what i wanted with the code
                Code:
                Dim x As Integer() = New Integer(10) {}
                
                    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                
                        Dim m_blnUsed As Boolean() = _
                        New Boolean(x.GetUpperBound(0)) {}
                
                
                        Dim objrandom As Random = New Random()
                        Dim intrandom As Integer
                        Dim index As Integer
                        Do
                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                            
                        Loop Until m_blnUsed(intrandom) = False
                        m_blnUsed(intrandom) = True
                
                
                        For index = 0 To 5
                            x(index) = intrandom
                        Next
                
                        Button3.Text = Str(x(0))
                
                        Do
                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                
                        Loop Until m_blnUsed(intrandom) = False
                        m_blnUsed(intrandom) = True
                
                
                        For index = 0 To 5
                            x(index) = intrandom
                        Next
                        Button4.Text = Str(x(1))
                        Do
                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                
                        Loop Until m_blnUsed(intrandom) = False
                        m_blnUsed(intrandom) = True
                
                
                        For index = 0 To 5
                            x(index) = intrandom
                        Next
                        Button5.Text = Str(x(2))
                        Do
                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                
                        Loop Until m_blnUsed(intrandom) = False
                        m_blnUsed(intrandom) = True
                
                
                        For index = 0 To 5
                            x(index) = intrandom
                        Next
                        Button6.Text = Str(x(3))
                        Do
                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                
                        Loop Until m_blnUsed(intrandom) = False
                        m_blnUsed(intrandom) = True
                
                
                        For index = 0 To 5
                            x(index) = intrandom
                        Next
                        Button7.Text = Str(x(4))
                        Do
                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                
                        Loop Until m_blnUsed(intrandom) = False
                        m_blnUsed(intrandom) = True
                
                
                        For index = 0 To 5
                            x(index) = intrandom
                        Next
                        Button8.Text = Str(x(5))
                
                    End Sub
                
                End Class
                i'm having this on immediate window
                though at run time i'm still getting A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                A first chance exception of type 'System.IndexOu tOfRangeExcepti on' occurred in 2nd ind asgmt.exe
                i'm more concern about the next stage which i don't have any clue, even how to start
                what i want is the first of the buttons containing the selected numbers clicked, the number will be copied in the first of a 6 set of labels and so on.
                to be more explicit, the 6 buttons will be clicked one at a time and their content copied in the 6 labels as going; the 1st button to be clicked to the 1st label, so on
                Last edited by Modibbo; May 4 '07, 07:16 PM. Reason: correction

                Comment

                • Modibbo
                  New Member
                  • Apr 2007
                  • 33

                  #9
                  i've been thinking (may be wrongly & sorry about that incase) there might be a way to set up an array of captions and assign them to the labels and i've been trying hard, but again i have no clue
                  any help pleasethis what i've been trying to
                  Code:
                  doDim y As CaptionButton() = New CaptionButton() {}
                  
                  label1.text="y(0)"
                  label2.text=y(1 )and i'm getting the error message
                  Declaration expected
                  at both the last 2 lines of the code (where i'm trying to assign the captions to the labels

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    What about if you just set up a form-level variable to count the number of times buttons have been clicked. Each time a button is clicked (up to 6, or whatever) you add 1 to that counter and use it to decide which label to update.

                    As for the way you're assigning the values to the buttons, it's good to see that you got it working, but I believe you can use a bit more loop processing to reduce the size of the code considerably. Remember, the basic idea behind a loop is that you should not need to do something more than once. Anything which you are coding repeatedly (for example, your For x = 0 to 5 loop setting up the x array) can usually be done inside a loop instead.

                    Comment

                    • Modibbo
                      New Member
                      • Apr 2007
                      • 33

                      #11
                      thanks once more again for helping, as i was saying i don't have any clue as to how to start, would it be possible to give an example of code please
                      thanks

                      Comment

                      • Modibbo
                        New Member
                        • Apr 2007
                        • 33

                        #12
                        i've been trying to set up a string array and assign the contents to each of the labels as needed with the code
                        Code:
                         Dim y As String() = New String() {}
                        
                        Label1.Text="y(0)"
                        and getting the error message
                        Declaration expected
                        with a wigly line on Label1

                        Comment

                        • SammyB
                          Recognized Expert Contributor
                          • Mar 2007
                          • 807

                          #13
                          Originally posted by Modibbo
                          i've been trying to set up a string array and assign the contents to each of the labels as needed with the code
                          Code:
                           Dim y As String() = New String() {}
                           
                          Label1.Text="y(0)"
                          and getting the error message with a wigly line on Label1
                          You must have deleted Label1

                          Comment

                          • Modibbo
                            New Member
                            • Apr 2007
                            • 33

                            #14
                            Originally posted by SammyB
                            You must have deleted Label1
                            unless if i don't get you, but i did not delete Label1 as i'm telling you the wigly line is on Label1, so it has to be there to have the wigly line on it

                            Comment

                            • SammyB
                              Recognized Expert Contributor
                              • Mar 2007
                              • 807

                              #15
                              Originally posted by Modibbo
                              unless if i don't get you, but i did not delete Label1 as i'm telling you the wigly line is on Label1, so it has to be there to have the wigly line on it
                              No, it has to not be there to have a wigly line. :D When you hover over the wigly line, what is the error message?

                              Comment

                              Working...