开发者

Difference between : adding a tlb as reference to invoke function VS using 'Run'?

Assume I have a .Net library with a class(TestClass) w开发者_JAVA百科ith a function 'Add'. I am accessing this in Excel by writing a VBA addin. I want to know the difference between the following two ways of accessing FUN1.

  1. Add the tlb as reference and use New as in

    Dim obj = New MyLibrary.TestClass

    returnval = obj.Add(5,5)

  2. Regasm that .Net dll and use Run method as in

    returnVal = Run("Add", 5, 5);

I have a lot of code where I see this similar way of accessing a function and am really confused how this works.

How does 2 work, will it work or what is neccesary to make 2 work like this.


You need to replace the = with the As keyword for the Dim statement in your code snippet. This works (assuming that the class is registered properly with COM):

Sub AccessExternalUdf()
    Dim obj As New MyNamespace.MyClass
    result1 = obj.MyMethod    
    result2 = Run("MyMethod")
End Sub

The difference (as stated above by @Hans) is early versus late (Run) binding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜