7 Replies - 755 Views - Last Post: 22 February 2013 - 09:46 AM Rate Topic: -----

#1 stuckinthemud40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-February 13

Stuck with arrays not working using string values or numeric.

Posted 17 February 2013 - 02:58 PM

I am asking to be shown how to create the correct array for collecting the names of the players that will be used in four other forms. I am sure once I have a sold array that is working correctly I will be able to create the array I will need for the Points per player. I have wasted the last three days going thru the book and creating the sample programs for a better understanding of what I am doing wrong. This is due by the end of the week and the teacher feels it is wrong to answer questions on assignments. I have nowhere else to seek an answer. I have attach my code in the event it may be useful.


Public Class frmPlayers

    Dim PlayerNumber As Integer



    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click

        lstPlayerNames.Items.Clear()

    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click

        Me.Hide()
        frmMain.Show()

    End Sub

    
    Private Sub btnCreate_Click(sender As System.Object, e As System.EventArgs) Handles btnCreate.Click
        
        Dim intCount As Integer = 0 
        Dim intTotalNumberOfPlayers As Integer
        Dim PlayerNameArray As String

        ' creates an array to store the Names of the Players

        Do While intCount < intTotalNumberOfPlayers loop until value match

            intTotalNumberOfPlayers = InputBox("Enter the Player's Name.", "Player " & intCount) 'ask for player's name
            
            lstPlayerNames.Items.Add(PlayerNameArray(intCount)) ' listbox for Player Name to been used across four forms
            intCount += 1 'advance the count by a factor of 1
        Loop
    End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: Stuck with arrays not working using string values or numeric.

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: Stuck with arrays not working using string values or numeric.

Posted 17 February 2013 - 03:12 PM

Are you saying that you've spent 3 days studying arrays and have made no progress..? Creating an array is straight-forward, and there are several ways. For example,

Dim players(10) As String
players(0) = "Bob"    'storing the first element


Here are the docs.

As a matter of fact you probably should use a List but perhaps your assignment requires the use of arrays.
Was This Post Helpful? 0
  • +
  • -

#3 stuckinthemud40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-February 13

Re: Stuck with arrays not working using string values or numeric.

Posted 18 February 2013 - 08:34 AM

I have a basic understanding as far as declaring the array. The book gives the type of samples as you give. The assignment has no limit on the number of players to be allowed. All attempts to get a working model fail. I am not sure as to why because I can not get a solid answer when I ask about arrays. you response is basically all I ever get. I realize that it is a straight forward idea and if I did not has a mental disability that effects my ability to understand new ideas and concepts it would not be hard at all to do. The program is required to ask via by a looped input box the number of players followed by their names. Because of this requirement hard coding the names is not very useful. I have the concept of the for next loop has being to most practical one to use in this case.


View Postandrewsw, on 17 February 2013 - 03:12 PM, said:

Are you saying that you've spent 3 days studying arrays and have made no progress..? Creating an array is straight-forward, and there are several ways. For example,

Dim players(10) As String
players(0) = "Bob"    'storing the first element


Here are the docs.

As a matter of fact you probably should use a List but perhaps your assignment requires the use of arrays.

Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Stuck with arrays not working using string values or numeric.

Posted 18 February 2013 - 09:23 AM

Well, you really need to do some research into arrays, and fundamentals of VB.Net in general. Here's a partial list of things I see in your code that are going to give you problems.

Line 25: An array is declared as Dim arrayname() As String. your array is declared as a simple string variable. So no, you don't "have a basic understanding as far as declaring the array".

Line 25: You are declaring PlayerNameArray inside the btnCreate.Click handler, which means that it will not be visible anywhere except in that subroutine.

Line 29: When you arrive here, intTotalNumberOfPlayers = 0, so the Do While loop will never execute. As well, you stated that there is no limit on the number of players, so using a total number of players to end the loop will not work. Also, you have forgotten to place a comment character (') before the word loop.

Line 31 is not asking for a player name. The return value from the InputBox is being assigned to an Integer. You probably want to assign it to PlayerNameArray(intCount)

That will do for starters. Let us know if you have any more problems.
Was This Post Helpful? 0
  • +
  • -

#5 stuckinthemud40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-February 13

Re: Stuck with arrays not working using string values or numeric.

Posted 20 February 2013 - 03:25 PM

View Postlar3ry, on 18 February 2013 - 09:23 AM, said:

Well, you really need to do some research into arrays, and fundamentals of VB.Net in general. Here's a partial list of things I see in your code that are going to give you problems.
I did not say I had a "full understanding" of arrays just a basic idea based off of instructor's input. I am sorry if I gave that impression. I do manage to get something to populate the listbox but get errors with I try to make use of the index. for ex. inputing the name john as a player without the intcount variable the program records the full name and if the intcount variable is used it only records j.
Sub playerNameArray()

        Dim intCount As Integer = 0         ' Loop counter
        Dim intPlayerNumber As Integer = 1
        Dim strPlayers() As String         ' To hold players names
        Dim intNumPlayers As Integer           ' To hold the number of players

        ' /**
        intNumPlayers = InputBox("Number of Players on the Team", "Size of team")

        If intNumPlayers < 2 Then                                                               ' change to a try parse 
            MessageBox.Show("Team must have more then 1 player")
            intNumPlayers = InputBox("Number of Players on the Team", "Size of team")
        End If

        ' /**

        Do Until intCount = intNumPlayers
            strPlayers(intCount) = InputBox("Player name", "Name of Player " & intPlayerNumber) ' null error with this line
            ' strPlayers = InputBox("Player name", "Name of Player " & intPlayerNumber) ' no error given
            lstPlayerNames.Items.Add(strPlayers)
            intCount += 1
            intPlayerNumber += 1
        Loop


    End Sub



possible to pull this array onto another form's listbox?



Line 25: An array is declared as Dim arrayname() As String. your array is declared as a simple string variable. So no, you don't "have a basic understanding as far as declaring the array".

Line 25: You are declaring PlayerNameArray inside the btnCreate.Click handler, which means that it will not be visible anywhere except in that subroutine.

Line 29: When you arrive here, intTotalNumberOfPlayers = 0, so the Do While loop will never execute. As well, you stated that there is no limit on the number of players, so using a total number of players to end the loop will not work. Also, you have forgotten to place a comment character (') before the word loop.

Line 31 is not asking for a player name. The return value from the InputBox is being assigned to an Integer. You probably want to assign it to PlayerNameArray(intCount)

That will do for starters. Let us know if you have any more problems.


View Postlar3ry, on 18 February 2013 - 09:23 AM, said:

Well, you really need to do some research into arrays, and fundamentals of VB.Net in general. Here's a partial list of things I see in your code that are going to give you problems.
I did not say I had a "full understanding" of arrays just a basic idea based off of instructor's input. I am sorry if I gave that impression. I do manage to get something to populate the listbox but get errors with I try to make use of the index. for ex. inputing the name john as a player without the intcount variable the program records the full name and if the intcount variable is used it only records j.

Sub playerNameArray()

        Dim intCount As Integer = 0         ' Loop counter
        Dim intPlayerNumber As Integer = 1
        Dim strPlayers() As String         ' To hold players names
        Dim intNumPlayers As Integer           ' To hold the number of players

        ' /**
        intNumPlayers = InputBox("Number of Players on the Team", "Size of team")

        If intNumPlayers < 2 Then                                                               ' change to a try parse 
            MessageBox.Show("Team must have more then 1 player")
            intNumPlayers = InputBox("Number of Players on the Team", "Size of team")
        End If

        ' /**

        Do Until intCount = intNumPlayers
            strPlayers(intCount) = InputBox("Player name", "Name of Player " & intPlayerNumber) ' null error with this line
            ' strPlayers = InputBox("Player name", "Name of Player " & intPlayerNumber) ' no error given
            lstPlayerNames.Items.Add(strPlayers)
            intCount += 1
            intPlayerNumber += 1
        Loop


    End Sub



possible to pull this array onto another form's listbox?



Line 25: An array is declared as Dim arrayname() As String. your array is declared as a simple string variable. So no, you don't "have a basic understanding as far as declaring the array".

Line 25: You are declaring PlayerNameArray inside the btnCreate.Click handler, which means that it will not be visible anywhere except in that subroutine.

Line 29: When you arrive here, intTotalNumberOfPlayers = 0, so the Do While loop will never execute. As well, you stated that there is no limit on the number of players, so using a total number of players to end the loop will not work. Also, you have forgotten to place a comment character (') before the word loop.

Line 31 is not asking for a player name. The return value from the InputBox is being assigned to an Integer. You probably want to assign it to PlayerNameArray(intCount)

That will do for starters. Let us know if you have any more problems.

Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Stuck with arrays not working using string values or numeric.

Posted 20 February 2013 - 09:14 PM

Please be careful with your quoting. You responded inside the quoted text, and managed to duplicate the whole post.

Anyway, have a look at this code, modified from the last code you posted.

    Sub playerNameArray()

        Dim intCount As Integer = 0         ' Loop counter
        Dim intPlayerNumber As Integer = 1
        Dim strPlayers As String()
        Dim intNumPlayers As Integer           ' To hold the number of players

        intNumPlayers = CInt(InputBox("Number of Players on the Team", "Size of team"))

        If intNumPlayers < 2 Then                                                               ' change to a try parse 
            MessageBox.Show("Team must have more then 1 player")
            intNumPlayers = CInt(InputBox("Number of Players on the Team", "Size of team"))
        End If

        ReDim strPlayers(intNumPlayers - 1)

        Do Until intCount = intNumPlayers
            strPlayers(intCount) = InputBox("Player name", "Name of Player " & intPlayerNumber) ' null error with this line
            lstPlayerNames.Items.Add(strPlayers(intCount))
            intCount += 1
            intPlayerNumber += 1
        Loop

    End Sub


The Redim statement is necessary because you did not provide the size of the array (you won't know that until the user puts in the number of players). When you do this, the array has been declared but not instantiated. In other words, it is NOTHING. That's why you were getting the null error about "No instance of an object".

When adding to the ListBox, you need to add a string, but you were trying to add an array of strings.

This post has been edited by lar3ry: 20 February 2013 - 09:16 PM

Was This Post Helpful? 1
  • +
  • -

#7 stuckinthemud40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-February 13

Re: Stuck with arrays not working using string values or numeric.

Posted 22 February 2013 - 08:48 AM

I want to thank you. I can make sense about the changes you show. I do have few more questions I would like to ask you. 1) the line showing the redim , could have been done when the variable was first declared? 2)the most effective method of copying the array to another form in the program is to create the array as a public array or copy the data from one array to another?
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Stuck with arrays not working using string values or numeric.

Posted 22 February 2013 - 09:46 AM

View Poststuckinthemud40, on 22 February 2013 - 09:48 AM, said:

1) the line showing the redim , could have been done when the variable was first declared?

Of course, but what size would you kae an array before you know how many players are on the team? You said that it could be for any size team. Some sports have ver large teams, so making it to hole, say 50 players would work for most sports, but a doubles team in Tennis would not need that much storage.

The ReDim after the InputBox makes sense.

Is there some reason you don't want to use it?

Quote

2)the most effective method of copying the array to another form in the program is to create the array as a public array or copy the data from one array to another?

There are many ways to get an array from another form. The best way is probably to have a function on the input form that implements the creation of the team list, and returns the array to the caller.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1