microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.74k stars 148 forks source link

VBScript engine dates, year, month and functions #450

Closed IrunguMunene closed 1 year ago

IrunguMunene commented 1 year ago

``Hi,

I have an app that receives vbscript snippets that should be executed and return values. Most of the snippets deal with dates, but also numbers and strings. I tried to execute the following code but the results of the Year function are wrong. How to deal with dates in VBScript while using ClearScript?

Secondly, I want to declare my own functions using VBScript in ClearScript and call them from other snippets. Is this possible?

`using (VBScriptEngine scriptEngine = new VBScriptEngine(NullSyncInvoker.Instance)) { scriptEngine.HostWindow = new HostWindow();

            // How would I call this function while executing other code
            scriptEngine.Execute(@"
                Public Function IIF(blnExpression, trueResult, falseResult)
                    If blnExpression Then
                        IIF = trueResult
                    Else
                        IIF = falseResult
                    End If
                End Function
            ");

            scriptEngine.Execute("Dim [Birth Date]");
            scriptEngine.Execute(@"[Birth Date] = 01/01/1990");

            var generation = scriptEngine.Evaluate(@"Year([Birth Date])");

            scriptEngine.Evaluate("MsgBox([Birth Date])");
        }`
ClearScriptLib commented 1 year ago

Hi @IrunguMunene,

I tried to execute the following code but the results of the Year function are wrong.

The problem is with the VBScript code [Birth Date] = 01/01/1990, It sets the variable to the numeric result of dividing 1 by 1990. Try this instead: [Birth Date] = CDate("01/01/1990").

I want to declare my own functions using VBScript in ClearScript and call them from other snippets. Is this possible?'

Sure, as long as you use the same engine to define your functions and run those snippets.

Good luck!

IrunguMunene commented 1 year ago

Thanks a lot.