Forum Discussion

fayrehouse's avatar
fayrehouse
Frequent Contributor
12 years ago

Does anyone know anything of Callbacks / Delegates in Jscript?

I have an an external DLL that provides a method I need to use that takes 2 params - a string, and an object of class"x" (according to the code completion prompt in TC).



In the documentation and sample code (typically C#, nothing JScript) I've seen in various places on line, this parameter is actually a delegate (bool) passed in as a function - which then returns a bool, and NOT an object of class "x".



Given my lack of knowledge of callbacks and delegates in JScript, it's not overly surprising that this, in my TC Jscript is erroring!



I see (again from TC code completion) that class "X" has a method "CreateDelegate" - it actually has 10 overloads of this method... but the first overload takes two params:

CreateDelegate(System.Type type, System.Reflection.MethodInfo method)



Forgive this rambling post - but if anyone can help - I'd desperately appreciate it!



  • I don't know specifically about JScript, but VBScript for example is not strongly typed. A lot of .Net types simply do not exists in VBScript. So when I have to interface to a .NET library from VBScript, I often have to write a COM-visible wrapper library in .NET first, and call that wrapper library from my script.



    ' Some .NET class function that returns a List(Of String), which is incompatible with standard VBScript

    Public Function DoSomethingInDotNet() As List(Of String)

        ' ...

    End Function



    ' Intermediate wrapper class function that returns a type compatible with VBScript (in this case an ArrayList)

    Public Function WrapDotNetFunction() As ArrayList

        Return New ArrayList(DoSomethingInDotNet())

    End Function



    ' VBScript code where you call the wrapper, which then calls the actual .NET method.

    Sub Main()

        Set Result = CreateObject("System.Collections.ArrayList")

        Set Result = objWrapper.WrapDotNetFunction

    End Sub
  • hlalumiere's avatar
    hlalumiere
    Regular Contributor
    I don't know specifically about JScript, but VBScript for example is not strongly typed. A lot of .Net types simply do not exists in VBScript. So when I have to interface to a .NET library from VBScript, I often have to write a COM-visible wrapper library in .NET first, and call that wrapper library from my script.



    ' Some .NET class function that returns a List(Of String), which is incompatible with standard VBScript

    Public Function DoSomethingInDotNet() As List(Of String)

        ' ...

    End Function



    ' Intermediate wrapper class function that returns a type compatible with VBScript (in this case an ArrayList)

    Public Function WrapDotNetFunction() As ArrayList

        Return New ArrayList(DoSomethingInDotNet())

    End Function



    ' VBScript code where you call the wrapper, which then calls the actual .NET method.

    Sub Main()

        Set Result = CreateObject("System.Collections.ArrayList")

        Set Result = objWrapper.WrapDotNetFunction

    End Sub
  • fayrehouse's avatar
    fayrehouse
    Frequent Contributor
    Thanks Hugo,



    I think I know what I need to do - I'll give it a go - if I get stuck, I'll drop another line here! 



    Cheers



    Steve