how can a math operator be set as a variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrew

    how can a math operator be set as a variable?

    Public sValue1 As String, sValue2 As String
    Public dValue1 As Double, dValue2 As Double
    Public vOper

    vOper = "*" 'set operator
    variable to "multiply"

    sValue1 = txtMain1.Text 'get first string
    value
    sValue2 = txtMain2.Text 'get second string
    value

    dValue1 = CDbl(sValue1) 'convert first string
    value to double
    dValue2 = CDbl(sValue2) 'convert second string
    value to double
    dResult = (dValue1 & vOper & dValue2)
    txtMain2.Text = CStr(dResult)



  • Auric__

    #2
    Re: how can a math operator be set as a variable?

    On Tue, 25 Jan 2005 01:12:32 GMT, "andrew" <hdg33@hotmail. com> wrote:
    [snip some code]


    I would start off by saying something in the body of the message.


    Option Explicit

    Public dValue1 As Double, dValue2 As Double
    Private sOper As String * 1

    Private Sub Form_Load()
    sOper = "*"
    dValue1 = Val(txtMain1.Te xt)
    dValue2 = Val(txtMain2.Te xt)

    dResult = Eval(dValue1, sOper, dValue2)
    txtMain2.Text = CStr(dResult)
    End Sub

    Function Eval(Val1 As Double, Op As String, Val2 As Double) As Double
    Select Case Op
    Case "+"
    Eval = Val1 + Val2
    Case "-"
    Eval = Val1 - Val2
    Case "*"
    Eval = Val1 * Val2
    Case "/"
    Eval = Val1 / Val2
    Case "^"
    Eval = Val1 ^ Val2
    Case Else
    Eval = 0
    ' or could raise an error
    End Select
    End Function

    --
    auric underscore underscore at hotmail dot com
    *****
    When you say "I wrote a program that crashed Windows", people just stare
    at you blankly and say "Hey, I got those with the system, *for free*".
    -- Linus Torvalds

    Comment

    Working...