Hello disco citizens. I'm trying to get a clear understanding of object oriented design principles, and my main interest is in generics and abstraction. The problem was always "This" until all of a sudden my school is sending me to vb.net class next semester. So i had to deal with "Me". Well, long story short, i think I'm starting to understand it. I'll post some code, it's fairly simple, a base class creates a concept, then the next one provides some functionality, which is then utilized by the Gui. There's two implementations of the base class, and very sparse ones at that.
First the Base Class, chanceObject.vb
Then the implementation i did first, coin.vb
And a die object was an obvious choice to complement
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
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!
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!