Public Array

Taien

Member
Joined
Oct 8, 2008
Messages
9
Programming Experience
1-3
I'm fairly new to VB and I am making a game as part of my portfolio project in a class. I'm having some trouble with array declaration though. Here's my code:

VB.NET:
Public varSkillLearnedArray(100) As Boolean
varSkillLearnedArray() = {True,true,true}

I have option explicit on and option strict on. When I type that second line in the code box, I get "Declaration Expected" in the error box. If I try using the following, I get "Explicit initialization is not permitted for arrays declared with explicit bounds."

VB.NET:
Public varSkillLearnedArray(100) As Boolean = {True, True, True}

This is all within a class. Do I need to declare public arrays outside the class? Because it seems to work fine if I declare them within a sub, but then I can't make them public, of course. Grrr. ;)

Help!

P.S. I intend to declare one hundred and one "false" values for the variable initially, but I can't even get this to work. ;)
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
First realize that your declaration of 100 would actually create 101 array items.

The below creates a boolean array with 5 items.

VB.NET:
[COLOR="Blue"]Dim [/COLOR]varSkillLearnedArray [COLOR="blue"]As Boolean[/COLOR]() = [COLOR="blue"]New Boolean[/COLOR](4) {[COLOR="blue"]True[/COLOR], [COLOR="blue"]True[/COLOR], [COLOR="blue"]False[/COLOR], [COLOR="blue"]True[/COLOR], [COLOR="blue"]False[/COLOR]}
 

Taien

Member
Joined
Oct 8, 2008
Messages
9
Programming Experience
1-3
Thanks Tom.

The only problem with that is that I want to be able to define how many items are in the array from the beginning, which I can't do if I declare the contents. But then again, I suppose declaring the contents automatically establishes the size, eh? ;)

A friend of mine recommended throwing the array in a module along with all my other variables, and I had no idea what a module was until today. Like I said, I'm just learning. ;) But I'm totally going to use the module, because it just made my variable system like ten times more efficient.
 

Solitaire

Well-known member
Joined
Jun 28, 2004
Messages
465
Location
New York
Programming Experience
10+
You could declare the array by size in the Module, and then assign values to each index in the array inside the main body, something like this:


Module Module1

Public varSkillLearnedArray(99) As Boolean

Sub Main()
For x As Integer = 0 To 2
varSkillLearnedArray(x) = True
Next x
End Sub

End Module
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
You can initialize the index upper bound in only one place.

If you specify an upper bound in the parentheses following the array variable name, you cannot use a New clause.

If you specify the upper bound in the parentheses in the New clause, you must leave the parentheses following the variable name empty.

However you can see that I did specify an upper bound limit after the New clause.

If you must add the limit in the variable, then you will have to initalize similar to the example provided by Solitare.
 

Taien

Member
Joined
Oct 8, 2008
Messages
9
Programming Experience
1-3
So, since in the code Solitaire provided the sub is within the module, it is called when the form is loaded, so the values should already be initialized and permanent at that point, correct? (well, I mean permanent until I change them ;))

The reason I ask is because I want to use the array as a big container for skill values in my game. There are going to be around 100 skills, if not more (and yes I did realize that 100 means 101 because of the zero that arrays init at), and I want to be able to keep track of which ones the character knows and which ones he doesn't with one array, and the value of each skill with another array. But I also read somewhere that I could create a multi-dimensional array by using something like "public varSkillArray()()" so that I could store both fields of information in the one array. For what I'm doing, would you suggest using two arrays or just one multi-dimensional one?

Like I said, I really I appreciate the help. I'm very new to this and although I have an aptitude for math and programming, some things aren't implicit. Haha, look, a programming joke. God, I'm a dork.
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
Yes you can use an multi-dimensional array, a user defined structure array, a class object, XML or an dataset.

I would say if you are using arrays, a multi-dimensional one may meet your needs better then comparing to seperate one-dimesion arrays.
 
Top Bottom