My FIRST Class!! But it doesn't work as expected - please HELP!



I have used VB3 - VB6, so learning all this OO stuff is reasonably new
to me (although I looked at Java a few years ago). Anyway, I thought I
would write a small class to begin with, with a property and 2 methods,
along with a constructor (where you use New(), I think, yes?).

So, I decided to create a class that represents a die, which can be
rolled - using a Roll() method - and which has a property to find out
the value on the face of the die after a roll - GetValue(). I then
added a Display() method that prints out the value of the die to the
console window.

Now, my thinking was that in writing the New() constructor I should
Roll() the die to get a 1st random number value. Indeed this worked
fine. However, when I instantiated (or do I say consumed?) the class
twice - the dice always show the same value!!!

I cannot see where this is going wrong - could someone please point out
the error of my ways?

The code is quite small - and here is the class (in Class1.vb)...

Public Class Die

Private _value As Int16
Dim _generator As New Random

Public Sub New()
Me.Roll()
End Sub

Public ReadOnly Property GetValue() As Int16
Get
Return _value
End Get
End Property

Public Sub Roll()
_value = _generator.Next(1, 7)
End Sub

Public Sub Display()
Console.WriteLine(Me.GetValue)
End Sub

End Class

The Module1.vb file holds this code...

Module Module1

Sub Main()

Dim One As New Die
Dim Two As New Die

Do

One.Display()
One.Roll()
Two.Display()
Two.Roll()

Loop Until UCase(Console.ReadLine) = "Q"

End Sub

End Module

When run, the values are listed like this...

1
1

2
2

6
6

5
5

3
3

Why do they duplicate if they are supposed to be random - there is
either something in the Dim One/Two as New Die that I don't appreciate
or there is something in the Constructor New() that I don't understand.

Thanks in advance for any help.

.



Relevant Pages

  • VB.NET101 - Constructors and Methods (2)
    ... argument constructor is never printed. ... Sub Main ... Public Sub Sum ... Console.WriteLine("Value mSumOfTwo after entering empty constructor ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Error trying to display array member
    ... instantiates the array within the constructor, ... Inherits CTwoDimensionalShape ' A triangle is a 2d shape ... Public Sub New ... Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB.NET-101 Constructors and Methods
    ... > disappeared after adding the empty constructor. ... > 'Public Sub New ... > 'Public Sub New ... > 'Public Sub New(ByVal first As Integer, ByVal second As Integer, ByVal ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB.NET-101 Constructors and Methods
    ... An empty one is needed if you don't want to call the one with 2 paramters. ... You declared the other constructor, ... > Public Sub New ... > Public Sub New(ByVal firstNumber As Integer, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: My FIRST Class!! But it doesnt work as expected - please HELP!
    ... Public Sub New ... Public ReadOnly Property GetValueAs Int16 ... Public Sub Roll() ... Dim One As New Die ...
    (microsoft.public.dotnet.languages.vb)