Lesson 3 - RollingDie in VB.NET - Constructors and random numbers

Visual Basic .NET OOP RollingDie in VB.NET - Constructors and random numbers

In the previous lesson, First object-oriented application in VB - Hello object world, we programmed our first object-oriented application in Visual Basic .NET. We are already able to create new classes, insert fields, methods with parameters and return values into them. Today, we're going to start working on an arena in which two warriors will battle against each other. The battle will be turn-based and one warrior will deduct health points from the other one based on his attack and on the other warrior's defense. Essentially, we'll be simulating a board game, so we'll add a "die" to our game so as to add a bit of randomness to the game. We're also going to learn to define custom constructors.

We'll start by creating a new console application and naming it Arena. We will also add a new class to our project and name it RollingDie. Let's think about the fields we'll have to add to our die in order for it to function according to our current needs. We should be able to choose the number of sides. Typically 6 or 10 sides as is standard in this type of game. Our die will also need a random number generator. The .NET framework provides one for us, which includes the Random class for these sort of projects. Our class will now have two fields:

  • sidesCount of the Integer type
  • random of the Random type, where the random number generator will be stored.

Last time, for simplicity's sake, we set all the fields of our class as publicly accessible; however, in most cases, we don't want our fields to be modified externally. In this case, we will use the Private modifier. An field is from then on only accessible from the inside of the class and from the outside VB.NET treats it like it doesn't exist. When we design a class we usually set everything to Private and then we make Public only those class members we really need to expose. Our class should now look something like this:

''' <summary>
''' Class representing a die for a board game
''' </summary>
Public Class RollingDie

        ''' <summary>
        ''' Random number generator
        ''' </summary>
        Private random As Random
        ''' <summary>
        ''' Number of sides that the die has
        ''' </summary>
        Private sidesCount As Integer

End Class

–°onstructors

We would now only be able to set the value of the Public fields from outside of the class since the Private fields are not visible from the outside. Next, we will go over constructors. A constructor is a method that is called while an object instance is being created. It sets the internal state of the object and performs any necessary initializations. We'd create a die in the Module1.vb now like this:

Dim die As RollingDie = New RollingDie()

The RollingDie() "method" is calling the constructor. Since our class doesn't have a constructor, VB.NET automatically generated an empty method. However, we will now add a constructor to the class. We declare it as a method, but it doesn't have a return type. Also, the constructor method must have the name New. In the constructor, we'll set a number of sides to a fixed value and create an instance of the Random class. The constructor will look like this:

Public Sub New()
        sidesCount = 6
        random = New Random()
End Sub

If we create the die now, it'll have a sidesCount field set to 6 and a Random class instance will be stored in the random field. We'll print the number of sides to the console, so as to visually confirm that the value is there. It's not good to set the field as Public since we don't want to be able to change the number of sides once the die is created. We'll add a GetSidesCount() method to the class which returns the value of the sidesCount field. We have now successfully created a read-only field. Meaning that, the field is not visible and can only be read only by using the "GetSidesCount()" method, so it can't be changed from outside the class. VB.NET has other structures made for these purposes, but we'll go over them later. The new method would look something like this:

''' <summary>
''' Returns the number of sides the die has
''' </summary>
''' <returns>Number of sides the die has</returns>
Public Function GetSidesCount() As Integer
        Return sidesCount
End Function

Let's move on to Module1.vb so we could create our first die and print the number of its sides to the console:

Dim die As RollingDie = New RollingDie() ' the constructor is called right here
Console.WriteLine(die.GetSidesCount())
Console.ReadKey()

The output:

Console application
6

Now we have visual confirmation that the constructor had been called, but we still need to be able to specify how many sides our die will have. Let's add a parameter to our constructor:

Public Sub New(aSidesCount As Integer)
        sidesCount = aSidesCount
        random = New Random()
End Sub

Notice that we prefixed the parameter name with "a", otherwise it would have the same name as a field and cause an error. Let's go back to Module1.vb and add a value for this parameter in the constructor:

Dim die As RollingDie = New RollingDie(10) ' a constructor with a par. 10 is called
Console.WriteLine(die.GetSidesCount())
Console.ReadKey()

The output:

Console application
10

Everything works as expected. VB.NET will not generate an empty, aka parameterless, constructor, so we can't create a die without passing the parameter(s) it needs. We can, however, create another constructor, one that is parameterless. We'll set the number of sides to 6 since the user probably expects this value as default:

Public Sub New()
        sidesCount = 6
        random = New Random()
End Sub

Let's create 2 dice instances now, one with a parameter and one without (in Module1.vb):

Dim sixSided As RollingDie = New RollingDie()
Dim tenSided As RollingDie = New RollingDie(10)
Console.WriteLine(sixSided.GetSidesCount())
Console.WriteLine(tenSided.GetSidesCount())
Console.ReadKey()

The output:

Console application
6
10

VB.NET doesn't mind us having two methods with the same name as long as their parameters are different. Meaning that the New() method, which in this case is the constructor, is overloaded. This is a fact for all methods, not just constructors. Visual Studio offers overloaded versions of methods when we're typing them. We can scroll through method variants with the arrow keys. This "scroll-tool" is called IntelliSense. We can see our two constructor overloads on the list:

IntelliSense hints for overloaded methods in VB.NET

Many methods in .NET have several overloads. Take a look at the Remove() method on a String. It's good to look through function overloads, so you don't end up programming something what has already been done for you. E.g. the WriteLine() method, which you use for writing to the console, has 18 variants.

Now, let's go over how to name constructor parameters, in our case aSidesCount. If we were to write them out like this:

Public Sub New(sidesCount As Integer)
        sidesCount = sidesCount
        random = New Random()
End Sub

VB.NET can't tell the two apart. In this case, the parameter is assigned to the parameter again. We can refer to the current instance inside a class because it is stored in the this variable. A practical use of the Me variable would be to use it in a GiveToPlayer(Player player) method and have it call a player.PickUp­Die(Me) inside the instance. We'd essentially be passing itself, the concrete die instance with which we're working, to the other player by means of the Me keyword. We won't deal with any of the "player" stuff for now, but we will set things up to reference the current instance while setting fields:

Public Sub New(sidesCount As Integer)
        Me.sidesCount = sidesCount
        random = New Random()
End Sub

Using Me, we specified that the left variable, sidesCount, belongs to the instance. The right one is treated as a parameter by VB.NET. Meaning that we now have two constructors that allow us to create different dice.

Random numbers

Next, we'll define the Roll() method in the RollingDie class, which will return a random number from 1 to the number of sides. The method will be Public, meaning that it can be called from outside the class, and will have no parameters. The return value will be of the Integer type. We can obtain a random number by calling the Next() method on the generator. It has several overloads:

  • Next(): The Parameterless variant returns a random number in the entire range of the Integer data type.
  • Next(To): Returns a non-negative number lesser than the To bound. random.Next(100) therefore returns a number between 0 and 99.
  • Next(From, To): Returns a random number between specified interval. From still belongs to the interval because it is inclusive, but To does not, because it is exclusive. Therefore random.Next (1, 101) would return a random number from 1 to 100;

The third overload suits our purposes the most, so we'll write:

''' <summary>
''' Rolls a die
''' </summary>
''' <returns>A number from 1 to sides count</returns>
Public Function Roll() As Integer
        Return random.Next(1, sidesCount + 1)
End Function

Do not create a random number generator in the method returning the random number. It would create a new generator for every random number and would result in the numbers hardly ever being random if at all. Always create a single shared instance of the generator, e.g. as a private field using the constructor, and then call the Next() method on it.

Overriding the ToString() method

Our RollingDie class is almost ready, let's add one more method before we wrap up this class. The ToString() method that we've briefly gone over in previous lessons is contained in every object, including our die. The method is designed to return a textual representation of the instance. Which is handy in all cases where we need to print the instance or work with it as if it were text. Even numerical datatypes have this method.

We already know that VB.NET performs implicit conversions. When we want to print a number or any other object to the console, VB.NET calls the ToString() method and prints its return value. We should never make our own method such as Print() when there is already a way to handle string conversions in VB.NET. It makes no sense to use it in our die, but when implementing warriors it could return his/her names. Still, for education's sake, we'll override ToString() in our RollingDie class anyway. It'll say that it is a die and print how many sides it has. First, print a RollingDie instance directly into our console:

Console.WriteLine(sixSided)

Only the path to our class is printed to the console, which is RollingDie.

We can't just declare a ToString() method since it's already there (we'll find out why in an upcoming tutorial). We must override it. We won't go too far in detail for now, but I want you to know what the proper usage of ToString() is. We use the Overrides keyword for overriding:

''' <summary>
''' Returns a textual representation of our die
''' </summary>
''' <returns>Textual representation of the die</returns>
Public Overrides Function ToString() As String
        Return String.Format("Rolling a die with {0} sides", sidesCount)
End Function

Let's print out the die instance to the console again.

The output:

Console application
Rolling a die with 6 sides

Let's test our rolling die out now. We'll roll them in loops and see if they work as expected:

' Create instances
Dim sixSided As RollingDie = New RollingDie()
Dim tenSided As RollingDie = New RollingDie(10)

' Rolls the 6-sided die
Console.WriteLine(sixSided)
For i As Integer = 1 To 10
        Console.Write(sixSided.Roll() & " ")
Next

' Rolls the 10-sided die
Console.WriteLine(vbCrLf)
Console.WriteLine(tenSided)
For i As Integer = 1 To 10
        Console.Write(tenSided.Roll() & " ")
Next

Console.ReadKey()

The output should look something like this:

Console application
Rolling a die with 6 sides
3 6 6 1 6 3 6 2 6 3

Rolling a die with 10 sides
5 9 9 2 10 4 9 3 10 5

We've created a nice, customizable class that represents a die. It will come in handy when we get to the arena part of the application, but you can use it whenever you'd like. Hopefully, you now understand how OOP allows you to reuse components. In the next lesson, Reference and value data types in VB.NET, we'll talk about the differences between reference data types (objects) and value data types (e.g. Integer). :)


 

 

Article has been written for you by Michal Zurek
Avatar
Do you like this article?
1 votes
Activities (11)

 

 

Comments

Avatar
Stafford Campbell:26. April 1:02

Again a good lesson, but I had a problem with the Function containing the Random number generator function. For sixsided but not tensided it generated a result "False'

 
Reply 26. April 1:02
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

1 messages from 1 displayed.