Question

  • Thread starter Thread starter Kartic
  • Start date Start date
K

Kartic

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.

Please advise. Thanks.
 
I did this useing an enum and a function. Kinda long but it works.

Public Enum Ops
Add
Subtract
Multiply
Divide
....
End Enum

Public function PerformOperation(num1 as double, num2 as double, operation
as Ops)

Select case operation
Case Ops.Add
return num1 + num2
Case Ops.Subtract
return num1 - num2
...
End select
end function

hope it helps.
 
It still did not answer my question. Currently I am doing something like
your example.
Using this I still have to write if or case to check the operation type.

Any other suggestion.
Thanks, Kartic
 
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
 
Dear Kartic,

Without knowing much about what you are doing...

Perhaps delegates would be useful. Once created, you can call the
delegate with your arguments, whatever function it's going to perform.

Have a read about them. If nothing else, it's a fascinating topic :-)

Regards,
Fergus
 
Back
Top