January 28, 2021
Hot Topics:

Using Graphics: Making a Lander Game - Part 3

  • By John Percival
  • Send Email »
  • More Articles »

We'll be using a User Defined Type (UDT) to hold the data. This needs to hold the name and the score, so it would seems sensible to have two fields, one for the name and one for the score:

Private Type Score
  strName As String
  intScore As String
End Type

Private Scores() As Score

You may be wondering why there are no dimensions defined for the array. This is because the array is dynamic. In other words, we can change the size of the array as the program is running. This enables us to have a dynamic length table that can be changed by a property of the class, rather than having it hard coded into the class. To change the length of the array, we can use the ReDim statement.

As I mentioned, we will be using a property to control the table length. If you don't understand properties or classes, have a read of the ActiveX Tutorial and all will be revealed! Let's take a look at the code first, then I can explain it:

Private numberplaces As Integer
Public Property Get NumPlaces() As Integer
NumPlaces = numberplaces
End Property
Public Property Let NumPlaces(ByVal vNewValue As Integer)
numberplaces = vNewValue
ReDim Preserve Scores(1 To numberplaces) As Score
End Property

As with all properties, a private variable within the class holds the actual value of the property. The Get function is very simple, but the Let function is a little more tricky as the array must be resized. This is done using the ReDim statement. The Preserve keyword tells VB to try and keep all the data that is in the array intact. This works well when the array is made smaller, as VB can just get rid of the excess data, but when the array is enlarged, VB just fills the array with empty variables - not great, but it'll do.

We also need properties for the table name, and for the 'type' of high score table. The type of high score table determines whether a higher score is better, or a lower score is better. This could be achieved using Enums, which allow us to take advantage of VB's autocomplete features. Here's the code:

Private scorename As String
Private ScoreOrder As ScoreSystem
Public Enum ScoreSystem
BiggerIsBetter = 0
SmallerIsBetter = 1
End Enum
Public Property Get ScoreType() As ScoreSystem
ScoreType = ScoreOrder
End Property
Public Property Let ScoreType(ByVal vNewValue As ScoreSystem)
ScoreOrder = vNewValue
End Property
Public Property Get TableName() As String
TableName = scorename
End Property
Public Property Let TableName(ByVal vNewValue As String)
scorename = vNewValue
End Property

That's most of the boring bits out of the way now. Let's have some fun with the registry.

Page 3 of 6

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date