C
Cap'n Ahab
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.
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.