Results 1 to 7 of 7

Thread: Help with arrays

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    41

    Help with arrays

    So I have an assignment dealing with arrays for school but first I will start it off with the simple question as to where I am stuck now. I was wondering what the simplest way is to declare all the numbers in an array without going down the list one by one ( ArrayA(1) = 1, ArrayA(2) = 3 and so on.) I was wondering if the simplest way to do this is just by putting:
    ArrayA = ArrayA += 1 or what the simplest way to do this is. Thanks for any help guys I appreciate it.

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Help with arrays

    If the numbers follow a formula or pattern, you just use a loop. For--Next loops work best for these tasks since you know the starting and ending point of your loop, and the loop uses an indexing number. Example:

    Code:
    Dim myArray(99) As Integer
    For i As Integer = 0 to 99
        myArray(i) = i + 1
    Next
    This fills a 100 element array (0 through 99) with the number 1 to 100 (i + 1). Each cycle of the loop, I is incremented one higher. You can use any formula in these examples... maybe you need powers of 2:

    Code:
    Dim myArray(19) As Integer
    For i As Integer = 0 to 19
        myArray(i) = 2 ^ (i + 1)
    Next
    Now, you can also directly assign numbers to an array, as long as it's a dynamic array. You can do this:
    Code:
    Dim myArray() As Integer = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144}
    Last edited by Jenner; Feb 26th, 2011 at 02:59 PM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: Help with arrays

    Ahhh thank you very much Jenner. The for loop is exactly what I was trying to clarify. This should work perfectly for what I need to do. I am sure I will end up posting again on this project but thanks again for the help.

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: Help with arrays

    So I made ArrayA(10) and ArrayB(10) in the declarations before then I added the For Next Loop making the array go from 1 to 10 and am trying to view the output in a listbox but when I assign a button to

    Listbox.Items.Add(ArrayA)

    It gives me some wacked out answer: "int32[]" and that is all that displays in the listbox. Thanks to anyone who can help.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Help with arrays

    add is for 1 string, + an array is a collection of strings (or other datatypes) + you use addrange to add a collection:

    vb Code:
    1. Listbox.Items.AddRange(ArrayA)

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with arrays

    Quote Originally Posted by shea6892 View Post
    So I made ArrayA(10) and ArrayB(10) in the declarations before then I added the For Next Loop making the array go from 1 to 10 and am trying to view the output in a listbox but when I assign a button to

    Listbox.Items.Add(ArrayA)

    It gives me some wacked out answer: "int32[]" and that is all that displays in the listbox. Thanks to anyone who can help.
    The first entry in an array is (0). So ArrayA(10) is 11 elements (0-10).
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: Help with arrays

    I am having problems making sure that my arrays are containing the right information. I want to list them in a ListBox but List.Items.Add and List.Items.AddRange methods are both not working to display a value in the Listbox. I am just trying to check to make sure that the array values are correct and then be able to display the matrix in the listbox also. Thanks.
    This is my code so far:





    Code:
    Public Class Form1
    
        Dim ArrayA(10), ArrayB(10), Matrix(10, 4) As Double
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            For x As Double = 1 To 20
                ArrayA(x) = x + 2
            Next
    
            For x As Double = 0 To 20
                ArrayB(x) = x + 2
    
            Next
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        End Sub
    End Class
    Edit: I got it covered. Now I just need to format a string to create nice columns for the Matrix in my form.
    Last edited by shea6892; Feb 26th, 2011 at 11:23 PM.

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