Results 1 to 9 of 9

Thread: [RESOLVED] [2005] Simple Array break down please

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Resolved [RESOLVED] [2005] Simple Array break down please

    Hey guys,

    With only four months of VB experience under my belt as with many new controls I find myself not able to instantly comprehend the full use of a new control and arrays seem to be no different. I come to this forum for the experience that is offered and outside input that can see very very stupid newbie mistakes that I over look in trying to comprehend it.

    I have made a small array and For... Next statement and would like it if someone could just give me a simple break down line by line so I can get a starting point in understanding the use of the counter that enables to gather input from a user and put that information into the array. Feel free to help out.

    Code:
    Dim intArray(19) as Integer
    Dim howmany as Integer
    Dim count as integer
    
    For count = 0 to 19
    
    howmany = CInt(InputBox("Please input how many fish " & ControlChars.CrLf _
    & "you caught on each trip."))
    ListBox1.Items.Add(intArray(howmany))
    count += 1
    Next count
    Thanks
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  2. #2
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: [2005] Simple Array break down please

    I see a possible issue: you need not increment the counter manually by doing Count += 1. In the For...Next loop it is incremented automatically. In this case, it might be that the loop will run only 10 times. What else are you looking for?

  3. #3
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: [2005] Simple Array break down please

    i cant understand, please explain what you want exactly.

  4. #4

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] Simple Array break down please

    Good call on the automatic incremention of the counter. The loop only ran 10 times, my next task is to find out how to take the number from the input box and store the user input into the indexes of the array. When the loop runs the output is 0 instead of the number being put in the input box.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Simple Array break down please

    I see a bigger issue than that!
    vb Code:
    1. ListBox1.Items.Add(intArray(howmany))

    Howmany is the number that people are entering, not an index value. What you need is:

    vb Code:
    1. Dim intArray(19) As Integer
    2.         Dim howmany As Integer
    3.         Dim count As Integer
    4.  
    5.         For count = 0 To 19
    6.             'GET VALUE THAT USER ENTERS
    7.             howmany = CInt(InputBox("Please input how many fish " & ControlChars.CrLf _
    8.             & "you caught on each trip."))
    9.  
    10.             'ASSIGN THE VALUE HOW MANY TO YOUR intARRAY AT THE CURRENT POSITION (0, then 1, then 2 etc...)
    11.             intArray(count) = howmany
    12.  
    13.             'Place CURRENT intArray value into Listbox - it's the count index that tells us that
    14.             ListBox1.Items.Add(intArray(count))
    15.  
    16.             'NO NEED FOR 'COUNT += 1'
    17.         Next count
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Simple Array break down please

    You're adding elements from the array to the ListBox but you never put anything into the array, so all elements will always be zero. I was under the impression that we had addressed this same issue in another thread. Inside the loop you want to assign the value 'howmany' to the element at index 'count':
    vb Code:
    1. inatArray(count) = howmany
    You can then add that value to the ListBox in the loop too using:
    vb Code:
    1. ListBox1.Items.Add(howmany)
    or:
    vb Code:
    1. ListBox1.Items.Add(intArray(count))
    or else you can wait until the end of the loop and then add all the values in one go, which I did post to your other thread:
    vb Code:
    1. ListBox1.Items.AddRange(intArray)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] Simple Array break down please

    Boy talk about a go to guy...

    Stim nailed what I needed, I line by line shown by use of comments of what is going on. Sometimes I wish I could just carry you around in my back pocket man.

    Not to be out done, jmc gave a great explination also, thanks guys I'm going to be studying your replies and gaining a good working knowledge of whats going on here...

    God I LOVE this forum!
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  8. #8

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] Simple Array break down please

    Hey Stim how are you inputting your code into this forum? Are you using the VB tag or pasting?
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  9. #9
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] [2005] Simple Array break down please

    I just put the code in the VB tags. Although it's easier to read when in colour but I don't like the numbers. Is there an option where you can have colour but not numbers? It makes copy/pasting a pain.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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