|
-
Feb 26th, 2011, 01:45 PM
#1
Thread Starter
Member
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.
-
Feb 26th, 2011, 02:55 PM
#2
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.
-
Feb 26th, 2011, 04:43 PM
#3
Thread Starter
Member
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.
-
Feb 26th, 2011, 05:52 PM
#4
Thread Starter
Member
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.
-
Feb 26th, 2011, 06:28 PM
#5
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:
Listbox.Items.AddRange(ArrayA)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2011, 06:33 PM
#6
Re: Help with arrays
 Originally Posted by shea6892
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).
-
Feb 26th, 2011, 09:10 PM
#7
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|