Forum Discussion

Pakema's avatar
Pakema
Contributor
10 years ago

[TC 10.6|VBS] Eval and Execute - what wrong with it?

Hi there!

 

I have some duplicating code

 

f.e.

 

If a = b Then
Log.Error("A must be C!")
Exit Function
End If

 

 

I insert this code in variable

 

a = "" &_
"If a = b Then" & Chr(13)  & Chr(10)

a = a &_
"Log.Error(""A must be C!"")" & Chr(13) & Chr(10)

a = a &_
"Exit Function" & Chr(13) & Chr(10)

a = a &_
"End If" & Chr(13) & Chr(10)

and then when I try apply it in Eval (a) or Execute(a)  - script is terminated with an exception 

 

Why?

 

 

 

    • Pakema's avatar
      Pakema
      Contributor

      I have code in 3 functions

       

      Function F1
      
      Dim a

      Dim b

      a = 5

      b = 6
      If a = b Then Log.Error("A must be C!") Exit Function End If End Function ' F1 Function F2
      Dim a

      Dim b

      a = 3

      b = 0

      If a = b Then Log.Error("A must be C!") Exit Function End If End Function ' F2 Function F3
      Dim a

      Dim b

      a = 4

      b = 7
      If a = b Then Log.Error("A must be C!") Exit Function End If End Function ' F3

      And I want to simplify this situation, f.e.

       

      Function F1
      
      Dim a
      
      Dim b
      
      a = 5
      
      b = 6
      
      execute(F4) ' <-- Error: script is terminated with an exception 
      
      Function F2
      
      Dim a
      
      Dim b
      
      a = 3
      
      b = 0
      
      eval (F4) ' <-- Error: script is terminated with an exception 
      
      End Function ' F2
      
      Function F3
      
      Dim a
      
      Dim b
      
      a = 4
      
      b = 7
      
      execute(F4) ' <-- Error: script is terminated with an exception 
      
      End Function ' F3
      
      Function F4
      
      Dim LocalVar_TmpTxt
      
      Dim LocalVar_CharEnter
      
      LocalVar_CharEnter = Chr(13) & Chr(10)
      
      LocalVar_TmpTxt = LocalVar_TmpTxt &_
      "If a = b Then" & LocalVar_CharEnter & LocalVar_CharEnter 
      
      LocalVar_TmpTxt = LocalVar_TmpTxt &_
      "Log.Error(""A must be C!"")" & LocalVar_CharEnter & LocalVar_CharEnter 
      
      LocalVar_TmpTxt = LocalVar_TmpTxt &_
      "Exit Function" & LocalVar_CharEnter & LocalVar_CharEnter
      
      LocalVar_TmpTxt = LocalVar_TmpTxt &_
      "End If" & LocalVar_CharEnter & LocalVar_CharEnter
      
      F4 = LocalVar_TmpTxt
      
      End Function ' F4

      Why error: script is terminated with an exception ?

       

       

      • sbkeenan's avatar
        sbkeenan
        Frequent Contributor

        Hi Pakema

         

        It's not really clear what you are trying to achieve here, but I have given you a basic solution to what I think you are trying to achieve below.  It could be simplified further, but I don't want to lose you if you're just starting out!!

         

        Basically each sub (F1, F2 and F3) will need to be executed separately, each will pass their a and b values to F4, which will determine whether they are equal and display a suitable message.  The solution could easily be configured such that F4 is a function and returns a true or false value to each of the calling subs, which would then be able to deal with the result in their own way.  It could be further simplified whereby only one sub is required and it could simply loop through a specified number of iterations, each time passing different values to F4, but without knowing what you are trying to achieve, I've kept it to the same basic format that you gave.

         

        If you need further assistance, perhaps you could give us more specific details of exactly what it is you are trying to achieve.

         

        sub F1
        	dim a
        	dim b
        	a = 5
        	b = 6
        
        	call F4(a, b)
        end sub
        
        sub F2
        	dim a
        	dim b
        	a = 3
        	b = 0
        
        	call F4(a, b) 
        end sub
        
        function F3
        	dim a
        	dim b
        	a = 4
        	b = 7
        
        	call F4(a, b) 
        end function
        
        sub F4(a, b)
        	if a = b then
        		log.message("A and B are the same")
        	else
        		Log.error("A must be B!")
        	end if
        end sub

         

        Regards

        Stephen.