VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "cls_Exp_Build"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Type MyVariables
    strVarName As String
    strVarValue As String
    strVarType As String
End Type

Private EachVar() As MyVariables

Public Function Numerical_Evaluation(strExpression As String) As Double
Dim tmpString() As String
Dim lngLoop As Long
Dim tmpResult As Variant
Dim FirstValue As String
Dim tmpStrExp

FirstValue = Mid(strExpression, 1, InStr(1, strExpression, " ", vbBinaryCompare) - 1)
If Not IsNumeric(FirstValue) Then
    For lngLoop = LBound(EachVar) To UBound(EachVar)
        If FirstValue = EachVar(lngLoop).strVarName Then
            tmpStrExp = strExpression
            Exit For
        Else
            tmpStrExp = "0 " & strExpression
        End If
    Next lngLoop
            strExpression = tmpStrExp
End If

tmpString = Split(strExpression, " ", -1, vbBinaryCompare)

tmpResult = Get_Var_Value(tmpString(LBound(tmpString)))
    For lngLoop = LBound(tmpString) + 1 To UBound(tmpString)
        Select Case tmpString(lngLoop)
            Case Is = "+"
                tmpResult = Add(CDbl(tmpResult), CDbl(Get_Var_Value(tmpString(lngLoop + 1))))
                lngLoop = lngLoop + 1
            Case Is = "-"
                tmpResult = Diff(CDbl(tmpResult), CDbl(Get_Var_Value(tmpString(lngLoop + 1))))
                lngLoop = lngLoop + 1
            Case Is = "*"
                tmpResult = Mul(CDbl(tmpResult), CDbl(Get_Var_Value(tmpString(lngLoop + 1))))
                lngLoop = lngLoop + 1
            Case Is = "/"
                tmpResult = Div(CDbl(tmpResult), CDbl(Get_Var_Value(tmpString(lngLoop + 1))))
                lngLoop = lngLoop + 1
            Case Is = "Avrge"
                tmpResult = Avg(Get_Var_Value(tmpString(lngLoop + 1)))
                lngLoop = lngLoop + 1
            Case Else
                MsgBox "Exception Handler"
        End Select
    Next lngLoop
Numerical_Evaluation = tmpResult
tmpResult = 0
End Function

Public Property Get Get_Var_Value(uVariable As String) As String
Dim lngCounter As Long
For lngCounter = LBound(EachVar) To UBound(EachVar)
    If EachVar(lngCounter).strVarName = uVariable Then
       Get_Var_Value = EachVar(lngCounter).strVarValue
       Exit For
    End If
Next lngCounter

If Len(Get_Var_Value) = 0 Then
        Get_Var_Value = uVariable
End If
End Property
Public Property Let Set_Var_Value(uVariable As String, uValue As String)
Dim lngCounter As Long
For lngCounter = LBound(EachVar) To UBound(EachVar)
    If EachVar(lngCounter).strVarName = uVariable Then
       EachVar(lngCounter).strVarValue = uValue
       Exit For
    End If
Next lngCounter
End Property


Public Sub Add_New_Variable(ByVal uVarName As String, uVarType As String, Optional uVarValue As String)
        EachVar(UBound(EachVar)).strVarName = uVarName
        EachVar(UBound(EachVar)).strVarType = uVarType
        EachVar(UBound(EachVar)).strVarValue = uVarValue
        
        ReDim Preserve EachVar(UBound(EachVar) + 1)
End Sub

Private Sub Class_Initialize()
    ReDim EachVar(0)
End Sub

Private Function Add(uValOne As Double, uValTwo As Double) As Double
    Add = uValOne + uValTwo
End Function

Private Function Diff(uValOne As Double, uValTwo As Double) As Double
    Diff = uValOne - uValTwo
End Function

Private Function Mul(uValOne As Double, uValTwo As Double) As Double
    Mul = uValOne * uValTwo
End Function

Private Function Div(uValOne As Double, uValTwo As Double) As Double
    Div = uValOne / uValTwo
End Function

Private Function Avg(uValue As String) As Double
    Dim lngLoop As Long, tmpSum As Double
    Dim indVal() As String
    indVal = Split(uValue, ",", -1, vbBinaryCompare)
    For lngLoop = LBound(indVal) To UBound(indVal)
        tmpSum = tmpSum + CDbl(indVal(lngLoop))
    Next lngLoop
        Avg = tmpSum / CDbl(UBound(indVal) + 1)
End Function


