Return Value From VBScript In .NET

D

Derek Hart

I am using an MSScriptControl in vb.net to do an evaluation of a text
string:
tempAnswer = MyScriptControl.Eval(MyScriptString)

But how can I use this to get a return value from a vbscript function:

Function Foo() As Integer
If MyValue = 1 Then
Foo=1
Else
Foo=2
End
End Function

tempCode = "Code Above - Read From A Database"
tempAnswer = MyScriptControl.Run(tempCode)

Do I use the Run command. I do not see a return statement in vbscript. Can
this be done in vb.net?
 
U

urkec

Derek Hart said:
I am using an MSScriptControl in vb.net to do an evaluation of a text
string:
tempAnswer = MyScriptControl.Eval(MyScriptString)

But how can I use this to get a return value from a vbscript function:

Function Foo() As Integer
If MyValue = 1 Then
Foo=1
Else
Foo=2
End
End Function


The above is not valid VBScript code. You can not declare function return
type and the If statement needs the closing End if.

tempCode = "Code Above - Read From A Database"
tempAnswer = MyScriptControl.Run(tempCode)

Do I use the Run command. I do not see a return statement in vbscript. Can
this be done in vb.net?


MSScriptControl.Run() has a return value and you can use it to run a
VBScript function and get the return value:


Dim objSC As Object
objSC = CreateObject _
("MSScriptControl.ScriptControl")
objSC.Language = "VBScript"

Dim strCode As String
strCode = "Function Foo(intVal)" & vbCrLf _
& "If intVal = 1 Then " & vbCrLf _
& "Foo = 1 " & vbCrLf _
& "Else" & vbCrLf _
& "Foo = 2" & vbCrLf _
& "End If" & vbCrLf _
& "End Function"

objSC.AddCode(strCode)

Dim objRetVal As Object

For i = 0 To 20
objRetVal = objSC.Run("Foo", i)
Console.WriteLine(objRetVal)
Next

Console.ReadLine()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top