Results 1 to 7 of 7

Thread: Public Array

  1. #1
    Taien is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    9
    Reputation
    0

    Question Public Array

    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:

    Code:
    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."

    Code:
    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.

  2. #2
    Tom
    Tom is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2005
    Posts
    746
    Reputation
    374
    First realize that your declaration of 100 would actually create 101 array items.

    The below creates a boolean array with 5 items.

    Code:
    Dim varSkillLearnedArray As Boolean() = New Boolean(4) {True, True, False, True, False}

  3. #3
    Taien is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    9
    Reputation
    0
    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.

  4. #4
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    417
    Reputation
    162
    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

  5. #5
    Tom
    Tom is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2005
    Posts
    746
    Reputation
    374
    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.

  6. #6
    Taien is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    9
    Reputation
    0
    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.

  7. #7
    Tom
    Tom is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2005
    Posts
    746
    Reputation
    374
    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking