First the Base Class, chanceObject.vb
Public MustInherit Class chanceObject
Public Property Value As Integer
Public Property stringValue As String
Public Overrides Function ToString() As String
Return Me.stringValue
End Function
Public MustOverride Sub assignValue()
End Class
Then the implementation i did first, coin.vb
Public Class coin
Inherits chanceObject
Public Property boolValue As Boolean = False
Overridable Function doFlip()
Static x As New System.Random
Me.Value = x.Next(0, 2)
assignValue()
Return Me
End Function
Public Overrides Sub assignValue()
Select Case Me.Value
Case 0
stringValue = "Heads"
boolValue = False
Case 1
stringValue = "Tails"
boolValue = True
End Select
End Sub
End Class
And a die object was an obvious choice to complement
Public Class die
Inherits chanceObject
Public Property Sides As Integer
Overridable Function doRoll()
Static x As New System.Random
Me.Value = x.Next(1, Sides + 1)
assignValue()
Return Me
End Function
Public Overrides Sub assignValue()
stringValue = Me.Value
End Sub
End Class
If you throw the code in a forms application, put a button on the form, and a multiline textbox, with a vertical scrollbar in it.
then use this code to demo the classes
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.TextBox1.Text = ""
Dim myCoin As New coin
Dim myDie As New die
myDie.Sides = 6
For i = 0 To 25
myDie.doRoll()
myCoin.doFlip()
Me.TextBox1.Text += "Coin Result : " _
& vbCrLf & myCoin.ToString & vbCrLf
Me.TextBox1.Text += "Roll Result : " _
& vbCrLf & myDie.ToString & vbCrLf
Next
End Sub
End Class
My question is what is right and wrong in the above code? how can i improve on it? A big thanks to AdamSpeight2008 for the overrides ToString thing, that was really the key i needed to see a "Pathway" for a chanceObject hierarchy!
This post has been edited by doveraudio: 28 December 2012 - 07:00 PM

New Topic/Question
Reply




MultiQuote




|