Results 1 to 7 of 7

Thread: Answer not correct

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    12

    Angry Answer not correct

    Hi everyone,

    I have the following code, can anyone tell me why it is returning zero, cos i cannot figure it out.
    VB Code:
    1. Public A As Integer, B As Integer
    2. Public Formula As String
    3.  
    4. Public Function FormulaBasher() As String
    5.    
    6.     Dim FormulaScript As New ScriptControl
    7.    
    8.     FormulaScript.Language = "VBScript"
    9.    
    10.     A = 10
    11.     B = 5
    12.     Formula = "A+B"
    13.  
    14.     FormulaBasher = CStr(FormulaScript.Eval(Formula))
    15. End Function

    Thanks in advance





    Edit: Added [vbcode][/vbcode] tags for clairty. - Hack
    Last edited by Hack; Aug 25th, 2005 at 10:58 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Answer not correct

    Your string Formula simply contains "A+B", rather than the actual values "10 + 5".

    To put the values into the string you need to do this:
    VB Code:
    1. Formula = A & "+" & B

  3. #3
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Answer not correct

    How would the scriptcontrol know the value of A and B?
    You tell it to Add A and B but will the control use the A and B declared in your module or make it's own, local variables A and B?

  4. #4
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: Answer not correct

    I think that's still going to give a string, (10+5) as you're passing the plus-sign as a character. I reckon you have to run the Eval function on the same line as the assignment to Formula.
    I don't understand the purpose of the code though. Why not just declare A and B as integers and be done with it?

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Answer not correct

    The Eval function of the scriptcontrol performs the calculations specified in strings (such as "(27+4) *2/3^4" if you like), and returns the value of that calculation.

    jeroen79 is correct in that the scriptcontrol was creating its own variables A and B, which were both the default value (0).

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    12

    Re: Answer not correct

    If the eval function is creating and using local variables, how can i get it to use the global variables???

    I cannot create A and B as integers as i said before, because the formula could be ANYTHING!!! And can involve up to 14 variables!! A - L.

    Thanks again!

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Answer not correct

    You cannot get Eval to use your variables, as it is completely separate from the scope of your program. You somehow need to place their values in the string as I did previously.

    There is a simple solution though, you can use the Replace function to put the values into the string for you, eg:
    VB Code:
    1. Public A As Integer, B As Integer, C as Integer, ...
    2. Public Formula As String
    3.  
    4. Public Function FormulaBasher() As String
    5.         Dim FormulaScript As New ScriptControl
    6.    
    7.     FormulaScript.Language = "VBScript"
    8.     A = 10
    9.     B = 5
    10.     C = 3
    11.     '...
    12.  
    13.     Formula = "(A+B) * C + A"
    14.  
    15.     'Replace the variables with their values.
    16.     Formula = Replace(Formula, "A", A)
    17.     Formula = Replace(Formula, "B", B)
    18.     Formula = Replace(Formula, "C", C)
    19.     '...
    20.  
    21. 'temporary code, so you can see the changes have been made:
    22. MsgBox formula
    23.  
    24.     FormulaBasher = CStr(FormulaScript.Eval(Formula))
    25. End Function

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