Records within Arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Records within Arrays

  1. #1
    Join Date
    Feb 2006
    Posts
    6

    Question Records within Arrays

    Hi all,

    I want to make an array of players for a game. I need to know how to make records within an array, so that each value of the array contains a record which contains a number of variables on each player. Furthermore, I need the number of players to be variable, so the array needs to be variable in size (anything from 1 to 10 players). The array also needs to be accessed all over the program, which has several forms with numerous commands, subs and functions which will read and write to the records within the arrays.

    Any idea where I should start?

    Thanks
    Tenpoints

  2. #2
    Join Date
    Aug 2005
    Posts
    51
    There are several ways you could reach your objective, pehaps the simplest of which is to create your own Public datatype in a module that can then be accessed anywhere in your program.

    Basically you could do something like this:

    Public Type Player
    player_name As String
    player_age As Integer
    player_highScore As Long
    End Type

    There you have created your own datatype like a date or an integer or a string, but it contains the attributes YOU define.

    Next, you simply create an array of type Player and handle it as you would any other array.

    Dim PlayerArray(9) as Player 'creates array of Player type for 10 players

    You would then refer to the attributes of individual elements in your array as follows:

    PlayerArray(0).player_name = "Joe"
    PlayerArray(0).player_age = 12
    PlayerArray(0).player_highscore = 10000

    PlayerArray(1).player_name = "Fred"
    PlayerArray(1).player_age = 15
    PlayerArray(1).player_highscore = 50000

  3. #3
    Join Date
    Feb 2006
    Posts
    6
    Thanks for the advice. Will I need a module for every new datatype? I was also thinking of using this for a high scores chart, though I may be able to pull data from the big record. Still in the planning stages atm.

  4. #4
    Join Date
    Aug 2005
    Posts
    51
    You can declare multiple new datatypes on ONE module. Just use different names for each datatype.

  5. #5
    Join Date
    Feb 2006
    Posts
    11

    Could also do a dynamic array

    dim myarray() as variant
    redim myarray(x)

    Where x can be any number of players you want. With redim you can declare the array without a constant.

Similar Threads

  1. Replies: 0
    Last Post: 07-19-2002, 02:41 AM
  2. Replies: 15
    Last Post: 05-09-2001, 05:40 AM
  3. saving records in Access
    By Hian Chew in forum VB Classic
    Replies: 1
    Last Post: 03-28-2001, 01:45 PM
  4. savomg records in Access
    By Hian Chew in forum VB Classic
    Replies: 0
    Last Post: 03-28-2001, 11:25 AM
  5. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 12
    Last Post: 06-20-2000, 04:06 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center