Hello,
"Kartic" <kg****@csystem ssoftware.com> schrieb:
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(DoOperat ion(10, 10, Operators.Addit ion))
End Sub
Public Function DoOperation( _
ByVal x As Double, _
ByVal y As Double, _
ByVal Operator As IOperator _
) As Double
Return Operator.Perfor m(x, y)
End Function
..
..
..
Public Interface IOperator
Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double
End Interface
Public Class AdditionOperato r
Implements IOperator
Public Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double Implements IOperator.Perfo rm
Return x + y
End Function
End Class
Public Class SubtractionOper ator
Implements IOperator
Public Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double Implements IOperator.Perfo rm
Return x - y
End Function
End Class
Public Class MultiplicationO perator
Implements IOperator
Public Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double Implements IOperator.Perfo rm
Return x * y
End Function
End Class
Public Class DivisionOperato r
Implements IOperator
Public Function Perform( _
ByVal x As Double, _
ByVal y As Double _
) As Double Implements IOperator.Perfo rm
Return x / y
End Function
End Class
Public Class Operators
Private Shared m_Addition As AdditionOperato r
Private Shared m_Subtraction As SubtractionOper ator
Private Shared m_Multiplicatio n As MultiplicationO perator
Private Shared m_Division As DivisionOperato r
Shared Sub New()
m_Addition = New AdditionOperato r()
m_Subtraction = New SubtractionOper ator()
m_Multiplicatio n = New MultiplicationO perator()
m_Division = New DivisionOperato r()
End Sub
Public Shared ReadOnly Property Addition() As AdditionOperato r
Get
Return m_Addition
End Get
End Property
Public Shared ReadOnly Property Subtraction() As SubtractionOper ator
Get
Return m_Subtraction
End Get
End Property
Public Shared ReadOnly Property Multiplication( ) As
MultiplicationO perator
Get
Return m_Multiplicatio n
End Get
End Property
Public Shared ReadOnly Property Division() As DivisionOperato r
Get
Return m_Division
End Get
End Property
End Class
///
*huh*
HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet