Hello,
Kartic said:
Is there any way I can store +, -, *, / in some kind of variable/object
Something like this
dim xTransaction as object = "+"
Then Instead of writing 100 + 50, I write 100 xTransaction 50.
Otherwise I have to write lots of code each for adding, substracting,
multiplying and dividing.
\\\
Private Sub Test()
MsgBox(DoOperation(10, 10, Operators.Addition))
End Sub
Public Function DoOperation( _
ByVal x As Double, _
ByVal y As Double, _
ByVal Operator As IOperator _
) As Double
Return Operator.Perform(x, y)
End Function
..
..
..
Public Interface IOperator
Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double
End Interface
Public Class AdditionOperator
Implements IOperator
Public Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double Implements IOperator.Perform
Return x + y
End Function
End Class
Public Class SubtractionOperator
Implements IOperator
Public Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double Implements IOperator.Perform
Return x - y
End Function
End Class
Public Class MultiplicationOperator
Implements IOperator
Public Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double Implements IOperator.Perform
Return x * y
End Function
End Class
Public Class DivisionOperator
Implements IOperator
Public Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double Implements IOperator.Perform
Return x / y
End Function
End Class
Public Class Operators
Private Shared m_Addition As AdditionOperator
Private Shared m_Subtraction As SubtractionOperator
Private Shared m_Multiplication As MultiplicationOperator
Private Shared m_Division As DivisionOperator
Shared Sub New()
m_Addition = New AdditionOperator()
m_Subtraction = New SubtractionOperator()
m_Multiplication = New MultiplicationOperator()
m_Division = New DivisionOperator()
End Sub
Public Shared ReadOnly Property Addition() As AdditionOperator
Get
Return m_Addition
End Get
End Property
Public Shared ReadOnly Property Subtraction() As SubtractionOperator
Get
Return m_Subtraction
End Get
End Property
Public Shared ReadOnly Property Multiplication() As
MultiplicationOperator
Get
Return m_Multiplication
End Get
End Property
Public Shared ReadOnly Property Division() As DivisionOperator
Get
Return m_Division
End Get
End Property
End Class
///
*huh*
HTH,
Herfried K. Wagner