Array Newbie

Pace

Well-known member
Joined
Sep 2, 2005
Messages
90
Location
Manchester UK! :)
Programming Experience
1-3
Hi All,

Im pretty new to arrays... I understand their general concept IE they are a place for variable's, however I am having a bit of trouble understanding the following... can anyone talk me through how this works?

VB.NET:
 [SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] temperatures(6) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Prompt, Title [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Short
[/COLOR][/SIZE][SIZE=2]Prompt = "Enter the days temperature."
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] UBound(temperatures)
Title = "Day " & (i + 1)
temperatures(i) = InputBox(Prompt, Title)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Hey Pace,

VB.NET:
Dim temperatures(6) As Single 'Creates an array of 6 singles imagine it as

0 - 34degrees
1 - 45degrees
2 - 67degrees
3 - 20degrees
4 - 34degrees
5 - 50degrees

It's a collection of values these are all empty to start with and arrays always start at 0. If I filled the array as above then said "X = temperatures(2) " X would = 67.


PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Prompt, Title AsString
Dim i AsShort
Prompt = "Enter the days temperature."
For i = 0 To UBound(temperatures) 'This loops through the array starting at 0 (i will contain the numbers 0,1,2,3,4,5)

Title = "Day " & (i + 1) 'The + 1 here is needed because arrays start at 0
temperatures(i) = InputBox(Prompt, Title) 'Asks the user for a value for temperature number i
Next
EndSub

Hope this helps some.. bit rushed sorry.
 
Thanks badger!

The thing that was confusing me is this whole i = 0 to Ubound...

So say you had an array MyArray(32) ... you would still use i = 0 to Ubound to loop through all 32 entities in the array?
 
Yep thats it, UBound returns the upper bound or index of the array so you could use that or just specify 32 instead if you know how many there are in the array. The only thing to remember is that if you declare the array as temperatures(32) the first element is 0 and the last is 31. UBound(temperatures) would return 31.
 
Magic, thank you! :)
 
You must be thinking of C... in VB, when you declare temperatures(32), Ubound(temperatures) would return 32. The array would then be 0 through 32.
 
Good point mjb!

One thing I rather dislike about VB, didn't even realise it still did that in .Net!!

In which case Pace.. I'd advise you to use UBound(MyArray) - 1 in the loop.
 
I think I might be a bit confused :S

is it because if you did MyArray(32) it SHOULD be 0 - 31 but its 0 - 32?
 
It is just a difference between C/C++/C# (and other, similar languages) and VB. In C, when you declare myarray(32), the array contains 32 elements numbered 0 through 31. In VB, when you declare myarray(32), the 32 is the number of the last element in the array. So, it actually contains 33 elements, or 0 through 32. It is a language difference, that's all.
 
Thanks MJB, At least im getting my head round this :) to be honest its loads of fun :cool:
 
Back
Top