Reputation: 1049
I don’t think I’m going to be able to explain myself very well, so sorry if this doesn’t make sense.
The example below has two functions that does some calculations that work fine:
Sub Example()
Dim mValue = Multiply(2, 2)
Dim dValue = Divide(2, 2)
End Sub
Function Multiply(ByVal value1 As Double, ByVal value2 As Double) As Double
Return value1 * value2
End Function
Function Divide(ByVal value1 As Double, ByVal value2 As Double) As Double
Return value1 / value2
End Function
What I would like to do is something similar to the below though (this doesn’t work but hopefully this demonstrates what I’m trying to do). I want to share parameters with a class and then select a property that return a value, similar to DataGridView1.Item(1, 1).Size
for example. Sort of like a property inside a property, but I searched that and that didn't return any good results.
Sub example2()
Dim mValue = Values(2, 2).Multiply()
Dim dValue = Values(2, 2).Divide()
End Sub
Public Class Values
Public Shared _Value1, _Value2 As Integer
Public Sub New(ByVal Value1 As Double, ByVal Value2 As Double)
_Value1 = Value1
_Value2 = Value2
End Sub
Public Shared ReadOnly Property Multiply() As Double
Get
Return _Value1 * _Value2
End Get
End Property
Public Shared ReadOnly Property Divide() As Double
Get
Return _Value1 / _Value2
End Get
End Property
End Class
I assume what I am trying to do is possible, I just think I’m missing something small. Any help or direction would be great!
Upvotes: 0
Views: 224
Reputation: 180
Please correct if anything I missed , here I create class library
Namespace calulation
Public Class Operations
Private i_v1 As Integer
Private i_v2 As Integer
Private obj_mul As Multiply
Private d_value As Double
Public ReadOnly Property Multiply As Double
Get
obj_mul = New Multiply(i_v1, i_v2)
d_value = obj_mul.GetValues
Return d_value
End Get
End Property
Private obj_divide As Divide
Public ReadOnly Property Divide As Double
Get
obj_divide = New Divide(i_v1, i_v2)
d_value = obj_divide.GetValues
Return d_value
End Get
End Property
Public Sub New(ByVal v1 As Integer, ByVal v2 As Integer)
i_v1 = v1
i_v2 = v2
End Sub
End Class
Public Class Multiply
Private Shared s_value1 As Integer
Private Shared s_value2 As Integer
Public Sub New(ByVal v1 As Integer, ByVal v2 As Integer)
s_value1 = v1
s_value2 = v2
End Sub
Public Function GetValues() As Double
Return s_value1 * s_value2
End Function
End Class
Public Class Divide
Private Shared s_value1 As Integer
Private Shared s_value2 As Integer
Public Sub New(ByVal v1 As Integer, ByVal v2 As Integer)
s_value1 = v1
s_value2 = v2
End Sub
Public Function GetValues() As Double
Try
Return s_value1 / s_value2
Catch ex As Exception
Throw ex
End Try
End Function
End Class
End Namespace
Now put reference of that library to your windows form project
Imports calculation.calulation
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Dim obj As Double = New Operations(2, 3).Multiply
Dim obj2 As Double = New Operations(2, 3).Divide
MsgBox(obj.ToString)
MsgBox(obj2.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Upvotes: 0
Reputation: 54417
You need to decide whether you're going to use Shared
members or instance members. You have a constructor that is not Shared
so that means that you need to create an instance, which you do with the New
keyword. If you create an instance then you need instance members to access on that instance:
Public Class Values
Private value1 As Integer
Private value2 As Integer
Public Sub New(value1 As Integer, value2 As Integer)
Me.value1 = value1
Me.value2 = value2
End Sub
Public Function Multiply() As Integer
Return value1 * value2
End Function
Public Function Divide() As Double
Return value1 / value2
End Function
End Class
Sub Example()
Dim values = New Values(2, 2)
Dim product = values.Multiply()
Dim quotient = values.Divide()
End Sub
The alternative is to use all Shared
members and not create an instance at all. the data goes in via Shared
properties and out via Shared
methods:
Public Class Values
Public Shared Property Value1 As Integer
Public Shared Property Value2 As Integer
Public Shared Function Multiply() As Integer
Return Value1 * Value2
End Function
Public Shared Function Divide() As Double
Return Value1 / Value2
End Function
End Class
Sub Example()
Values.Value1 = 2
Values.Value2 = 2
Dim product = Values.Multiply()
Dim quotient = Values.Divide()
End Sub
In the first case, you would create a Values
instance where you wanted to use it and any other code that created a different instance would have no effect on that. In the second case, because there is only one set of properties for the class, you might set them in one place and then set them to something else in a second place, then call a method in the first place and expect the result to make use of the first set of property values. With only one set of fields, the methods will always use the last of values assigned to them, no matter where in the code that happened.
It's also worth noting that, if you are thinking of defining a class with all Shared
members, you probably ought to define a module instead. Modules are compiled as classes behind the scenes and they inherently prevent you creating an instance, so module members behave like Shared
class members. The difference is that you don't have to qualify module members with the name of the module, where you do have to qualify Shared
class members. Some people don't like that and incorrectly think that it breaks the rules of OOP. You still can qualify module members if you want to but classes force you to.
If you want a one-liner then you can do that with both instance members and Shared
members, but they will look a bit different. If you're using instance members then you need an instance, so you must invoke a constructor. There's no requirement to keep that object around though. You can use it and throw it away if you want, e.g.
Dim product = New Values(2, 2).Multiply()
That's slightly wasteful if you're performing multiple operations on the same numbers though, because you'll be creating multiple identical instance, e.g.
Dim product = New Values(2, 2).Multiply()
Dim quotient = New Values(2, 2).Divide()
If you're using Shared
members then you'd have to overload the methods to allow you to pass in values or not:
Public Class Values
Public Shared Property Value1 As Integer
Public Shared Property Value2 As Integer
Public Overloads Shared Function Multiply() As Integer
Return Value1 * Value2
End Function
Public Overloads Shared Function Multiply(value1 As Integer, value2 As Integer) As Integer
Values.Value1 = value1
Values.Value2 = value2
Return Multiply()
End Function
Public Overloads Shared Function Divide() As Double
Return Value1 / Value2
End Function
Public Overloads Shared Function Divide(value1 As Integer, value2 As Integer) As Double
Values.Value1 = value1
Values.Value2 = value2
Return Divide()
End Function
End Class
You can then pass in new values when you call a method or use the existing values, e.g.
Dim product = Values.Multiply(2, 2)
Dim quotient = Values.Divide()
Upvotes: 1