I am trying to wrap an instance of a created class with another that share the same super class in Visual Basic 2013. I'm new to VB and been struggling with this.
This is for a Java to VB conversion..
Code example of what I have written:
Then run a test with this:Code:' Abstract product creator Class Public MustInherit Class PizzaMaker ' Abstract Method to create a pizza Public MustOverride Function createPizza(ByRef size As Integer, ByRef crust As String) As Pizza Public Function orderPizza(ByRef size, ByRef crust) As Pizza Dim pizza As Pizza pizza = createPizza(size, crust) Return pizza End Function End Class ' Concrete factory Class Public Class MargheritaMaker Inherits PizzaMaker ' Override abstrat method in superclass Public Overrides Function createPizza(ByRef size As Integer, ByRef crust As String) As Pizza Dim pizza As New MargheritaPizza(size, crust) Return pizza End Function End Class ' Abstract component product Class Public MustInherit Class Pizza ' Consatant variables used for pizza size Public Const SMALL As Integer = 1 Public Const MEDIUM As Int16 = 2 Public Const LARGE As Int16 = 3 Public Const EXTRA_LARGE As Int16 = 4 ' Crust type of pizza Private crustType As String ' Size of the pizza Private size As Int16 ' Description of the pizza Public description As String = "Pizza" ' Abstract method to return the cost of the pizza Public MustOverride Function getCost() As Double ' Returns size description Public Function getSizeDescription() As String Dim desc As String ' Determin pizza size and return String description If (size = 1) Then desc = "Small" ElseIf (size = 2) Then desc = "Medium" ElseIf (size = 3) Then desc = "Large" Else desc = "Extra Large" End If Return desc End Function Public Function getCrust() As String Return crustType End Function ' Sets the pizza crust type Public Sub setCrust(ByRef crust) crustType = crust End Sub ' Returns the pizza size Public Function getSize() As Integer Return size End Function ' Set the size of our Pizza Public Sub setSize(ByVal i) size = i End Sub ' Returns the String description of the pizza Public Function getDescription() As String Return getSizeDescription() + " " + crustType + " " + description End Function End Class ' Concrete component product Class defining a Margherita Pizza Public Class MargheritaPizza Inherits Pizza 'Dim cost ' Constructor set's the Pizza size, crust type & description Sub New(ByRef size As Integer, ByRef crust As String) setSize(size) setCrust(crust) description = "Margherita Pizza" End Sub ' Returns the Pizza base cost based on it's size Public Overrides Function getCost() As Double Dim cost As Double If (getSize() = Pizza.SMALL) Then 'Console.Write("in if" & vbNewLine) cost = 9.5 ElseIf (getSize() = Pizza.MEDIUM) Then cost = 10.5 ElseIf (getSize() = Pizza.LARGE) Then cost = 11.5 ElseIf (getSize() = Pizza.EXTRA_LARGE) Then cost = 12.5 End If 'Console.Write("in if" * vbNewLine) Return cost End Function End Class ' Abstract component product decorator Class Public MustInherit Class PizzaDecorator Inherits Pizza ' Abstract method that returns decorator description Public MustOverride Overloads Function getDescription() End Class ' Concrete component product decorator Class (used as Object wrapper) Public Class Cheese Inherits PizzaDecorator Dim pizza ' As Pizza ' Check that the construtor paramaters are correct!!! Also check scope of variables!!! Sub New(ByVal pizz) pizza = pizz 'pizza.setSize(pizz.getSize()) 'pizza.setCrust(pizz.getCrust()) End Sub ' Returns cost of product by delegating call to wrapped objects Public Overrides Function getCost() As Double Dim cost ' As Double = pizza.getCost() If (pizza.getSize() = pizza.SMALL) Then Console.Write(" In cheese pizza = SMALL" & vbNewLine) cost += 0.1 ElseIf (pizza.getSize() = pizza.MEDIUM) Then cost += 0.2 ElseIf (pizza.getSize() = pizza.LARGE) Then cost += 0.3 ElseIf (pizza.getSize() = pizza.EXTRA_LARGE) Then cost += 0.4 End If Console.Write(" Pizza size = " + pizza.getSize().ToString & vbNewLine) Console.Write(" in end if" & vbNewLine) Return cost + pizza.getCost() End Function Public Overrides Function getDescription() As Object Return pizza.getDescription() + ", Extra Cheese" End Function End Class
I assumed I could create a MargheritaPizza objcet with Button1 click and assign a pizza object to pizza via it's factory method createPizzaCode:Public Class TestForm Public margheritaM ' As MargheritaMaker Public pizza ' As Pizza Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Dim m As New MargheritaMaker() 'Dim pizza 'Dim margheritaM As New MargheritaMaker() margheritaM = New MargheritaMaker() pizza = margheritaM.createPizza(1, "Deep Pan") MargheritaBox.AppendText(pizza.getDescription() & vbNewLine) End Sub Private Sub CheeseButton_Click(sender As Object, e As EventArgs) Handles CheeseButton.Click 'pizza As New Cheese(pizza) pizza = New Cheese(pizza) MargheritaBox.AppendText(pizza.getDescription() & vbNewLine) End Sub Private Sub CostButton_Click(sender As Object, e As EventArgs) Handles CostButton.Click MargheritaBox.AppendText(pizza.getCost() & vbNewLine) End Sub Private Sub PepperoniButton_Click(sender As Object, e As EventArgs) Handles PepperoniButton.Click pizza = New Pepperoni(pizza) MargheritaBox.AppendText(pizza.getDescription() & vbNewLine) End Sub End Class
then on cheeseButton click I could wrap the pizza object created with the call pizza = New Cheese(pizza)!! The cheese class encapsulates the extra topping for the pizza. I thought that I could then call cost on the original pizza object wiich would delegate the cost through the wrapped objects?? As in the Decorator Pattern.
Some screen shot of output below:
Here I clicked create pizza then calculate cost then extra cheese finally calculate cost, all seems well!
This time I also clicked extra cheese and calculate cost, but the cost is not delegating through the objects correctly!
Here I added several extra cheese and also show some output to the console window for the test, the console window shows the size of the pizza each time the object is wrapped and shows that pizza size is 0 except on the inner most wrapper...What am I doing wrong??
My first language is Java and have no problem with this technique in the past, but new to VB 2013 and appreciate some help here..
All constructive comments welcome.
Many thanks
Please ignore code that's been commented out during testing




Reply With Quote
