Results 1 to 10 of 10

Thread: Dynamically Evaluating Expressions in vb6

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    Folks,

    Does anybody know how I can dynamically evaluate a maths expression that is held in a string.

    i.e.

    text1="10/3"
    ans = eval(text1)
    'ans is now 3.3333

    The eval function works in ASP 3.0 but doesn't work with vb6 is there an equivalent function I can use in vb?

    note: I' dont have ASP 3.0 so I can't include a reference.

    Cheers
    John

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    [code]
    Private Sub Command1_Click()
    'don't use the quotes ["10/3"]
    Text1 = 10 / 3
    MsgBox Val(Text1)
    End Sub
    [code]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    oops

    Sorry HeSaidJoe, but I just put in the line
    text1="10/3"
    as an example
    text1 is a text box that can be edited so I don't know whats going to be in there hence

    ans = eval(text1.text)

    if I just use val(text1.text) and text1="10/3" then I get an answer of 10 !!!!

    John

  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    This is actually a very tough question, and the code to go behind it all will be quite complicated. You see, to do it properly you have to take into account Brackets and operator precedence. You then have to do the right calculation on each pair of operators, unless it is a maths function that takes one operator, and work out the result. Harder than it sounds at first.

    Good Luck!
    Iain, thats with an i by the way!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    Iain17

    I know what you mean, this would involve some mental recursion and some form of stack data structure. I was kinda hoping for a function (in a dll or somewhere) along the lines of ASP 3.0's eval or execute function.

    John

  6. #6
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Alternitivley use the Microsoft Scripting Control (Goto the Project menu, hit components and select the Microsoft Script Control)

    Add the new control to your form, and add this code

    Code:
    Private Sub Command1_Click
    MsgBox ScriptControl1.Eval("3/6")
    End Sub

    Tada

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    Sam F,

    No Joy, I don't seem to have the Microsoft Scripting Control on my system (not in components) there is a Ms Scripting runtime dll in the references but that has no eval fuction (searched with object browser).

    Do you have win2000 or iis v5 on your system?

    I'll have a look on msdn.microsoft for the Scripting Control, I have iexplorer v5 you'd think that would install it.

    Cheers
    John

  8. #8
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Bit strange, I've got Win2K but I had the scripting control under win 98 as well, in fact I'm pretty sure IE uses it, just as a check open a copy of notepad and put this in it

    Code:
    VERSION 5.00
    Object = "{0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}#1.0#0"; "msscript.ocx"
    Begin VB.Form Form1 
       Caption         =   "Form1"
       ClientHeight    =   3195
       ClientLeft      =   60
       ClientTop       =   345
       ClientWidth     =   4680
       LinkTopic       =   "Form1"
       ScaleHeight     =   3195
       ScaleWidth      =   4680
       StartUpPosition =   3  'Windows Default
       Begin MSScriptControlCtl.ScriptControl ScriptControl1 
          Left            =   210
          Top             =   1545
          _ExtentX        =   1005
          _ExtentY        =   1005
          AllowUI         =   -1  'True
       End
       Begin VB.CommandButton Command1 
          Caption         =   "Command1"
          Height          =   495
          Left            =   870
          TabIndex        =   0
          Top             =   390
          Width           =   2265
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    
    Private Sub Command1_Click()
    MsgBox ScriptControl1.Eval("3/6")
    End Sub

    then save it as Form1.frm and open it with VB, see If it works, if it does then it means you have the control, if not then download it from somewhere.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    Thanks Sam

    But still no go - Errors in form load refer to form.log
    Like so
    .....
    Line 2: Could not create reference: '{0E59F1D2-1FBE-11D0-8FF2-00A0 D100 38BC}#1.0#0'.
    Line 13: Class MSScriptControlCtl.ScriptControl of control ScriptControl1 was not a loaded control class.
    Line 16: The property name _ExtentX in ScriptControl1 is invalid.
    Line 17: The property name _ExtentY in ScriptControl1 is invalid.
    Line 18: The property name AllowUI in ScriptControl1 is invalid.
    ......

    Obviously No control

    I'm using NT Server 4 -SP5 IIS4
    I think there is an upgrade to IIS5 which should have it.
    Strangly I have the regular expression object on my system which is supposed to be part of IIS5 ??

    But like you I thought ie5 would install it.

    Cheers
    John

  10. #10
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Sam,

    Well i'll be god damned. I wish someone had told me about that about 8 months ago when i asked.

    Anyway, thanks for pointing that out.
    Iain, thats with an i by the way!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width