How Do I Create an Extension of a Single Class Property - vb.net

I have a primitive Class that looks like this:
Public Class BaseGeoData
Property GeoOrigin As String
Property GeoDestination As String
Property TravelDistance As Double?
Property TravelTime As Double?
Public Sub New()
End Sub
End Class
Public Class GeoData
Inherits BaseGeoData
Public Sub New(geoOrigStr As String, geoDestStr As String)
GeoOrigin = geoOrigStr
GeoDestination = geoDestStr
TravelDistance = 5000 'in meters
TravelTime = 360 'in minutes
End Sub
End Class
I want to be able to add 2 extensions that will return converted values like this:
TravelDistance.ToMiles()
TravelTime.ToHours()
When I add a Module to extend the class, it offers the extension to the entire class, most properties of which will never use the extension. How can I just offer the extensions to the properties that need them?

Introduce own type of "Unit" for measurement values
Public MustInherit Class Unit
Public ReadOnly Property Value As Double
Public MustOverride ReadOnly Property Name As String
Public Sub New(value As Double)
Me.Value = value
End Sub
Public Overrides Function ToString() As String
Return $"{Value} {Name}"
End Function
End Class
Public Class Meter
Inherits Unit
Public Sub New(value As Double)
MyBase.New(value)
End Sub
Public Overrides ReadOnly Property Name As String
Get
Return "m"
End Get
End Property
End Class
Public Class Mile
Inherits Unit
Public Sub New(value As Double)
MyBase.New(value)
End Sub
Public Overrides ReadOnly Property Name As String
Get
Return "mi"
End Get
End Property
End Class
And extension methods for creating unit and convertions
Public Module UnitConversions
<Extension>
Public Function Meters(value As Integer) As Meter
Return New Meter(value)
End Function
<Extension>
Public Function Miles(value As Integer) As Mile
Return New Mile(value)
End Function
<Extension>
Public Function ToMiles(meters As Meter) As Mile
Dim miles = meters.Value * 0.00062137
Return New Mile(miles)
End Function
<Extension>
Public Function ToMeters(miles As Mile) As Meter
Dim meters = miles.Value * 1609.344
Return New Meter(meters)
End Function
End Module
Then you can use value in more readable manner
TravelDistance = 5000.Meters() ' meters
' Conversion
geoData.TravelDistance.ToMiles() ' miles
Console.WriteLine(geoData.TravelDistance) ' print 3.10685 mi

You can only add extension methods into types (i.e. classes).
TravelDistance is of type Double? so you have to add an extention method into Double?.
Note that it would make the method available for every Double?, which may not be something you want.

I really like Plutonix's resolution and is the same one I would go for first.
Its simple and resolves your initial problem.
Public Class BaseGeoData
Property GeoOrigin As String
Property GeoDestination As String
Property TravelDistance As Double?
Property TravelTime As Double?
Public Sub New()
End Sub
End Class
Public Class GeoData
Inherits BaseGeoData
Public Sub New(geoOrigStr As String, geoDestStr As String)
GeoOrigin = geoOrigStr
GeoDestination = geoDestStr
TravelDistance = 5000 'in meters
TravelTime = 360 'in minutes
End Sub
Function DistanceMiles() As Double
DistanceMiles = (TravelDistance/1609.344)
End Function
Function TimeHours() As Double
DistanceMiles = (TravelTime /60)
End Function
End Class

Related

How to determine why my code is not working?

Imports Kerry_Sales_Project
' base class
Public Class Bonus
Public Property SalesId As String
Public Property Sales As Double
' default constructor
Public Sub New()
_SalesId = String.Empty
_Sales = 0
End Sub
' parameterized constructor
Public Sub New(ByVal strId As String, ByVal dblSold As Double)
SalesId = strId
Sales = dblSold
End Sub
' GetBonus method
Public Overridable Function GetBonus() As Double
Return _Sales * 0.05
End Function
Public Shared Widening Operator CType(v As PremiumBonus) As Bonus
Throw New NotImplementedException()
End Operator
End Class
' derived class
Public Class PremiumBonus
Public Property Sales As Double
Public Property strId As String
' default constructor
Public Sub New(strId As String)
MyBase.New()
End Sub
' parameterized constructor
Public Sub New(ByVal strId As String, ByVal dblSold As Double)
MyBase.New(strId, dblSold)
End Sub
' class method
Public Overrides Function GetBonus() As Double
Return MyBase.GetBonus() + (Sales - 2500) * 0.01
End Function
End Class
In effect, you forgot to place Inherits Bonus in the Premium Bonus class. It would be something like
Public Class PremiumBonus: Inherits Bonus
And the other thing is the CType operator overload.
Public Shared Widening Operator CType(v As PremiumBonus) As Bonus
Throw New NotImplementedException()
End Operator
This operator overload is not necessary because the PremiumBonus class is a child of Bunus and they are contained.

Advantage or Disadvantage between Two Class Modules

What would be the advantages or disadvantages of using Class1 instead of Class2?
The quantity information stored in each instance of the class will be adjusted up and down as needed (via the functions, and while it seems to make sense to me that I would only need to make these variables public so that they are visible from outside the class, I feel that there is most likely some reason that this shouldn't been done.
Class1
Option Explicit
Public Sequence As String
Public Quantity As Double
Public Sub AddQty(sAddQty As Double)
Quantity = Quantity + AddQty
End Sub
Public Sub SubQty(sSubQty As Double)
Quantity = Quantity - sSubQty
End Sub
Class2
Option Explicit
Private iSeq As String
Private iQty As Double
Public Property Get Qty() As Double
Qty = iQty
End Property
Public Property Let Qty(lQty As Double)
iQty = lQty
End Property
Public Property Get Sequence() As String
Sequence = iSeq
End Property
Public Property Let Sequence(lSeq As String)
iSeq = lSeq
End Property
Public Sub AddQty(sAddQty As Double)
iQty = iQty + AddQty
End Sub
Public Sub SubQty(sSubQty As Double)
iQty = iQty - sSubQty
End Sub
In terms of interfaces, the two are exactly equivalent, because public fields are exposed as Property members. If you added a 3rd class module and wrote this:
Implements Class1
You would be forced by the compiler to add these members:
Private Property Get Class1_Sequence() As String
End Property
Private Property Let Class1_Sequence(ByVal RHS As String)
End Property
Private Property Get Class1_Quantity() As Double
End Property
Private Property Let Class1_Quantity(ByVal RHS As Double)
End Property
Private Sub Class1_AddQty(sAddQty As Double)
End Sub
Private Sub Class1_SubQty(sSubQty As Double)
End Sub
If you added another class module and wrote this:
Implements Class2
You would be forced by the compiler to have essentially the exact same members:
Private Property Get Class2_Sequence() As String
End Property
Private Property Let Class2_Sequence(ByVal RHS As String)
End Property
Private Property Get Class2_Qty() As Double
End Property
Private Property Let Class2_Qty(ByVal RHS As Double)
End Property
Private Sub Class2_AddQty(sAddQty As Double)
End Sub
Private Sub Class2_SubQty(sSubQty As Double)
End Sub
When properties do nothing and there's no incentive to properly encapsulate their values, go ahead and have public fields.
However there's little need for AddQty or SubQty instance methods when the backing field exposes a Property Let accessor - one could simply do foo.Quantity = foo.Quantity + 2 instead. An API that appears to provide multiple ways to do the same thing, is a confusing API.
So what you do, is you define an explicit interface that defines the API you want to work with:
Public Property Get Quantity() As Double
End Property
Public Property Get Sequence() As String
End Property
Public Sub AddQty(ByVal value As Double)
End Sub
Public Sub SubQty(ByVal value As Double)
End Sub
And then make your class Implements this interface (say, ISomething), and the rest of the code works with this ISomething interface that only exposes the members you want it to be able to work with - and that excludes the class' Property Let members; the rest of the code only sees what it needs to see, and can only access what it needs to access.
Dim foo As ISomething
Set foo = New Something
'foo.Quantity = 42 ' illegal
Dim bar As Something
Set bar = foo
bar.Quantity = 42 ' ok
bar.AddQty 2
Debug.Print foo.Quantity ' should be 44

Implement same logic for diffrent objects as T

I suppose to use T but i am not sure how do it in proper way.
Let's consider following example.
Base class:
Public Class HtmlBase
Implements IGetInformation
Public Overridable Function IsExist() As Boolean Implements IGetInformation.IsExist
Throw New NotImplementedException()
End Function
Public Overridable Function GetIdByName(pName As String) As Integer Implements IGetInformation.GetIdByName
Throw New NotImplementedException()
End Function
End Class
Example classes which inherit from base class:
Public Class HtmlSubSection
Inherits HtmlBase
'--sometimes i have to overload to add additioinal parameter
Public Overloads Function isExist(subsection As String) As Boolean
Dim dlsubkategorie As New DataLayer.DALSubSection
Return dlsubkategorie.CheckIfSubSectionExist(subsection)
End Function
Public Overrides Function GetIdByName(subsectionName As String) As Integer
Dim dlget As New DataLayer.DALSubSection
Return dlget.GetSubSectionIdByName(subsectionName)
End Function
End Class
Public Class HtmlSection
Inherits HtmlBase
'sometime i have to overload to add additioinal parameter
Public Overloads Function IsExist(section As String) As Boolean
Dim dlsubkategorie As New DataLayer.DALSection
Return dlsubkategorie.CheckIfSectionExist(section)
End Function
Public Overrides Function GetIdByName(Name As String) As Integer
Dim dlsubkategorie As New DataLayer.DALSection
Return dlsubkategorie.GetSectionIdByName(Name)
End Function
End Class
As could be seen above two classes which inherits from base within their methods has same logic (sometimes i have to use additional parameter therefore overloads there, but are using diffrent DAL class to call. I would like to implement this logic in base class and for each just point to specific DAL. How to do that to not everytime in those classes write e.g:
Dim dlsubkategorie As New DataLayer.<DALSection>
Return dlsubkategorie.GetSectionIdByName(Name)
EDIT:
Htmlbase constructor's:
Sub New()
End Sub
Sub New(pId As Integer)
_Id = pId
End Sub
HtmlSubSection's constructors:
Sub New()
MyBase.New()
AvailableSentences = New List(Of HtmlSubSection_Sentence)
SelectedSentences = New List(Of HtmlSubSection_Sentence)
End Sub
Sub New(pId As Integer)
MyBase.New(pId)
End Sub
Sub New(pName As String)
_Name = pName
End Sub
Sub New(pId As Integer, pName As String)
MyBase.New(pId)
_Name = pName
End Sub
HtmlSection's constructors:
Sub New()
MyBase.New()
End Sub
Sub New(pId As Integer)
MyBase.New(pId)
End Sub
Sub New(pId As Integer, pName As String, pPosition As Integer)
MyBase.New(pId)
_Name = pName
_Position = pPosition
End Sub
Sub New(pName As String)
_Name = pName
End Sub
Sub New(pName As String, pPosition As Integer)
_Name = pName
_Position = pPosition
End Sub
You donĀ“t need generic types here. Just use Interfaces, Sub Classing and Polymorphism correctly.
New Interface IDAL which is implemented by DAL classes to get rid of different method names which take same parameters and do the same:
Public Interface IDAL
Function CheckIfSectionExist(section As string) As Boolean
Function GetSectionIdByName(section As string) As Integer
End Interface
Public Class DALSection
Implements IDAL
Public Function CheckIfSectionExist(section As string) As Boolean Implements IDAL.CheckIfSectionExist
...
End Function
Public Function GetSectionIdByName(section As String) As Integer Implements IDAL.GetSectionIdByName
...
End Function
End Class
Public Class DALSubSection
Implements IDAL
Public Function CheckIfSubSectionExist(subSection As string) As Boolean Implements IDAL.CheckIfSectionExist
...
End Function
Public Function GetSubSectionIdByName(subSection As String) As Integer Implements IDAL.GetSectionIdByName
...
End Function
End Class
Base class changed to abstract and the constructor now takes IDAL parameter. Function can now be executed polymorphic. Added a isExists(string) function to avoid overloading:
Public MustInherit Class HtmlBase
Implements IGetInformation
Public Property DAL as DataLayer.IDAL
Protected Sub New(dal as DataLayer.IDAL)
Me.DAL = dal
End Sub
Public Overridable Function isExist() As Boolean Implements IGetInformation.isExist
Return True
End Function
Public Overridable Function isExist(section As String) As Boolean
Return DAL.CheckIfSectionExist(Section)
End Function
Public Overridable Function GetIdByName(pName As String) As Integer Implements IGetInformation.GetIdByName
Return DAL.GetSectionIdByName(pName)
End Function
End Class
Client classes only need to give correct DAL to base class:
Public Class HtmlSubSection
Inherits HtmlBase
Public Sub New()
MyBase.New(New DataLayer.DALSubSection)
End Sub
End Class
Public Class HtmlSection
Inherits HtmlBase
Public Sub New()
MyBase.New(New DataLayer.DALSection)
End Sub
End Class
Basically it would be ideal if IGetInformation had a isExist method with an optional string parameter. This would save one unneccessary method in HtmlBase.

Default values of Class properties

I have a class, it looks like this:
Public Class DataPoint
Private _data As Integer
Private _locInText As Integer
Private _searchValue As String
Property Data As Integer
Get
Return _data
End Get
Set(value As Integer)
_data = value
End Set
End Property
Property LocInText As Integer
Get
Return _locInText
End Get
Set(value As Integer)
_locInText = value
End Set
End Property
Property SearchValue As String
Get
Return _searchValue
End Get
Set(value As String)
_searchValue = value
End Set
End Property
End Class
I then create another class using this class.
Public Class PaintData
Public Time As TimeSpan
Public Color As DataPoint
Public Job As New DataPoint
Public MaxCurrent As New DataPoint
End Class
I want to create default values of some of the properties, namly SearchValue and LocInText. To me, it makes sense to do this inside the class definition, because these are essentially constants.
Q1. Should I be doing it this way? If not, what is the proper technique.
Q2. I can't get the syntax right. Can you help?
Public Class PaintData
Public Time As TimeSpan
Public Color As DataPoint
Public Job As New DataPoint
Public MaxCurrent As New DataPoint
Color.LocInText = 4 '<----Declaration expected failure because I'm not in a method
Job.LocInText = 5 '<----Declaration expected failure because I'm not in a method
End Class
Thanks all
Give DataPoint a constructor:
Public Class DataPoint
Private _data As Integer
Private _locInText As Integer
Private _searchValue As String
Public Sub New(locInText as Integer)
_locInText = locInText
End Sub
'...
End Class
And use that:
Public Class PaintData
Public Time As TimeSpan
Public Color As New DataPoint(4)
Public Job As New DataPoint(5)
Public MaxCurrent As New DataPoint(6)
End Class
Alternatively you could use
Public Property Color As DataPoint = New DataPoint With {.LocInText = 4}
in your class definition. This syntax is arguably more readable than the constructor syntax.

Wrapping an object with another in VB 2013

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:
' 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
Then run a test with this:
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
I assumed I could create a MargheritaPizza objcet with Button1 click and assign a pizza object to pizza via it's factory method createPizza
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
Wrapping
Firstly, and this is only my opinion, the term wrapping are used when you're dealing with NotInheritable classes.
Example:
Public Class String2
Public Sub New(s As String)
Me.s = s
End Sub
Default Public ReadOnly Property Chars(index As Integer) As Char
Get
Return Me.s.Chars(index)
End Get
End Property
Public ReadOnly Property Length() As Integer
Get
Return s.Length
End Get
End Property
Private ReadOnly s As String
End Class
Base classes
Secondly, you're overcomplicating this. The following code are stripped to make it more readable. Note that this is not a final solution.
Start by defining a Crust and a Size enum.
Public Enum PizzaCrust As Integer
[Default] = 0
DeepPan = 1
End Enum
Public Enum PizzaSize As Integer
S = 0
M = 1
L = 2
XL = 3
End Enum
You only need two base classes, Pizza and Extra.
Public MustInherit Class Pizza
Public Property Crust() As PizzaCrust
MustOverride ReadOnly Property Description() As String
Public ReadOnly Property Extras() As List(Of Extra)
MustOverride ReadOnly Property Name() As String
Public Property Size() As PizzaSize
Public Function CalculateCost() As Decimal
Dim value As Decimal = Me.GetBaseCost(Me.Size)
For Each extr As Extra In Me.Extras
value += extr.Cost
Next
Return value
End Function
Protected MustOverride Function GetBaseCost(size As PizzaSize) As Decimal
End Class
Public MustInherit Class Extra
Public MustOverride ReadOnly Property Name() As String
Public MustOverride ReadOnly Property Description() As String
Public MustOverride ReadOnly Property Cost() As Decimal
End Class
Pizzas
Create a namespace named Pizzas and put all of your pizzas here.
Namespace Pizzas
Public Class Margarita
Inherits Pizza
Public Overrides ReadOnly Property Description() As String
Public Overrides ReadOnly Property Name() As String
Protected Overrides Function GetBaseCost(size As PizzaSize) As Decimal
Select Case size
Case PizzaSize.S
Return 10
Case PizzaSize.M
Return 15
Case PizzaSize.L
Return 20
Case PizzaSize.XL
Return 30
Case Else
Return 0
End Select
End Function
End Class
Public Class Quattro
Inherits Pizza
Public Overrides ReadOnly Property Description() As String
Public Overrides ReadOnly Property Name() As String
Protected Overrides Function GetBaseCost(size As PizzaSize) As Decimal
End Class
End Namespace
Extras
Finally, create a namespace named Extras and put all of the extras here.
Namespace Extras
Public Class Cheese
Inherits Extra
Public Overrides ReadOnly Property Cost() As Decimal
Public Overrides ReadOnly Property Description() As String
Public Overrides ReadOnly Property Name() As String
End Class
Public Class Ham
Inherits Extra
Public Overrides ReadOnly Property Cost() As Decimal
Public Overrides ReadOnly Property Description() As String
Public Overrides ReadOnly Property Name() As String
End Class
Public Class Pineapple
Inherits Extra
Public Overrides ReadOnly Property Cost() As Decimal
Public Overrides ReadOnly Property Description() As String
Public Overrides ReadOnly Property Name() As String
End Class
End Namespace
Usage
Now, to create a new Margarita pizza, one would do it like this:
Dim pizza As New Pizzas.Margarita()
pizza.Size = PizzaSize.L
pizza.Crust = PizzaCrust.DeepPan
pizza.Extras.Add(New Extras.Ham())
pizza.Extras.Add(New Extras.Cheese())