Results 1 to 8 of 8

Thread: [RESOLVED] Need help with Arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Resolved [RESOLVED] Need help with Arrays

    Hi i am trying to do this project for class and i have been stuck on all day.
    Here is the assignment

    Array Assignment
    Write a program that allows the user to enter up to 20 Pro team names and then displays them. The user doesn't have to enter 20 but they may not enter more than 20.

    The team names must be stored in an array.

    A For.....Next loop must be used to display the team names in a listbox.

    If the user attempts to enter more than 20 team names an error should be displayed "No space to record additional team name"

    The .clear and .focus features should be used to clear and reset the textbox after the record button is clicked.

    The array and the counter used as the index for the array should be declared globally.


    This is what my form looks like

    Name:  cacf28fb176000e428eef7935ee6a73a.jpg
Views: 253
Size:  18.4 KB


    Here is my code

    Code:
    Public Class Form1
        Dim named(19) As String
        Dim cnt As Integer = 0
    
    
    
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            named(cnt) = TextBox1.Text
            cnt = cnt + 1
            TextBox1.Text = Nothing
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim strcnt As Integer = 0
            Label2.Text = Val(cnt)
            If cnt <= 20 Then
                For strcnt = 0 To named.Length - 1
                    ListBox1.Items.Add(named(strcnt))
                Next
            Else
                ListBox1.Items.Add("No space to record additional team name")
            End If
    
        End Sub
    So in button 1 (the Record team name button) i set the array named(cnt) = textbox1.text.. CNT is the counter. Then add 1 to cnt for the next time the button is clicked. It will set named(1) = to the value in textbox1. Then clear the textbox1.text

    In button 2 (display button) i create a new variable called STRCNT, which is a other counter for the for next loop! Ok i then set label2 = to cnt.. (This will display the number of teams). I then use a for next loop to check if cnt is less then 20 or greater then 20. I then use a for next loop to display all values in named in the listbox (IF it is less then 20!!) If it is greater i display the text in the listbox "No space to record additional team name"...

    ALSO i havent done the reset button yet, plan to do that at the end.

    So when i run the program it works fine when i enter names, but when i click the display button it highlights the code "ListBox1.Items.Add(named(strcnt))"

    It says "Value cannot be null.Parameter name: item"

    Can anyone please help??

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,467

    Re: Need help with Arrays

    What is the value of `strcnt` when that line is executed? Is it what you expect it to be? Either it's not or your expectation is wrong. What's happening is that `strcnt` is the index of an array element that you haven't set yet. That means that `named(strcnt)` is Nothing and you cannot add Nothing to a ListBox. I'm guessing that `strcnt` is the index of the first element that you haven't set yet rather then the last that you have set.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Re: Need help with Arrays

    Thanks for the reply Jmcilhinney! But i am still confused on how i would fix that. I assumed that strcnt is 0 and will increase by 1 because the for next loop.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: Need help with Arrays

    Have you dealt with breakpoints and stepping? That's what you need to solve this. Put a breakpoint on the For Next line. When execution stops on the breakpoint, take a look at the named array? Does it hold what you expect? If so, then step forwards to the line to add an item. At that point, look at named(strcnt), does it hold what you expect? Keep going until the exception occurs. There are several issues with the code, but none of them seem likely to cause this problem. The cause lies elsewhere, probably in item #4.

    Issues I noted:

    1) You use Val(cnt). Val converts a string to a Double. Cnt is already an integer, so Val does nothing except convert it to a double, which you don't actually want. So, get rid of Val(). It does some odd things, anyways, so even if you wanted to convert a string to a double there are better ways.

    2) Since you never clear the listbox, if you ever press the button twice, you'll get undesirable results.

    3) You are testing for cnt <=20 in the wrong place. That test makes sense before adding to the array, but you do it when putting the values into the listbox. If you were to keep adding names with the code as you have, you would be able to run off the end of the array, at which point you will get an index out of range exception. You need to check cnt before adding to the array, because that's where an out of range issue can occur.

    4) Since you never encountered the exception noted in 3, then you must never have filled the array. So, there may be an issue when you add something to the listbox that doesn't exist. It doesn't exist because you never added to that slot in the array. With that type of array, I believe you'd be adding Nothing, which may not work for a Listbox.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Re: Need help with Arrays

    No we haven't learned breakpoints and stepping. We have learned to use Step in the for next loop? I Fixed other problems you told me.
    Code:
         Public Class Form1
        Dim named(19) As String
        Dim cnt As Integer = 0
    
    
    
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If cnt <= 20 Then
                named(cnt) = TextBox1.Text
                cnt = cnt + 1
          Else
               Listbox1.items.add("No space to record additional team name")
            End If
            TextBox1.Text = Nothing
    
        End Sub
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Label2.Text = cnt
            ListBox1.Items.Clear()
        End Sub
    I just dont understand how to display the items now.
    Also thanks for the quick reply!
    Last edited by Pompy; Feb 7th, 2015 at 04:10 PM.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: Need help with Arrays

    Well, it kind of depends on what the purpose of the exercise is. After all, you should be able to do this:

    ListBox1.AddRange(names)

    to put all the names into the listbox in one step. If you are learning about looping, then that might not be an acceptable answer.
    My usual boring signature: Nothing

  7. #7
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Need help with Arrays

    Quote Originally Posted by Shaggy Hiker View Post
    Well, it kind of depends on what the purpose of the exercise is. After all, you should be able to do this:

    ListBox1.AddRange(names)

    to put all the names into the listbox in one step. If you are learning about looping, then that might not be an acceptable answer.
    From OP:
    A For.....Next loop must be used to display the team names in a listbox.
    Thus I'm guessing that it is partially a practice of a For loop as well as practice for using arrays.

    I'd also put the code for displaying the current cnt in the Label in Button1's code after adding the name to the array so that the user can see the running count when recording the names instead of when displaying the names. Also you may consider displaying your error message in a MessageBox rather than in the ListBox, since that is normally how you'd see error messages, but since the assignment doesn't actually stipulate that, I guess it really is up to you. As for adding the names to the ListBox, you really had it fine using your For loop; I'm just not really sure why you were getting the error that you were before.... One thing you might try is since cnt has the number of items in the array + 1 (because you add 1 after adding a name to the array), why not use cnt-1 in your For loop's condition instead of named.Length-1. The .Length property of arrays can sometimes be misleading. For example, this will always be 20 even though you may have only added a couple names to the array. Additionally you could check if the element in the array actually contains something before adding it to the ListBox. For example:
    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Label2.Text = cnt
            ListBox1.Items.Clear()
    
            For counter As Integer = 0 To cnt-1
                If Not String.IsNullOrEmpty(named(counter)) Then Me.ListBox1.Items.Add(named(counter))
            Next
        End Sub

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Re: Need help with Arrays

    Thanks for the repsonse Shaggy Hiker and Pyth007.

    I got it to work with the cnt - 1
    Code:
            For Strcnt As Integer = 0 To cnt - 1
                ListBox1.Items.Add(named(Strcnt))
            Next

    Thanks for everyone help with this!!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width