Calling c# .net Dll in VBA compile error

Hello.  I created a simple c# .net DLL with just one function called "addTwoLongs".  When I try to call the routine in VBA, I get the following compile error:

Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic.

Here is my code in c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace MyTestDLL
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class MyClass
    {
        public long addTwoLongs(long a, long b)
        {
            return (a + b);
        }
    }
}


Here is my code in VBA (using Excel 2010):


Public Function AddTheNumbers(ByVal num1 As Long, ByVal num2 As Long) As Long

    Dim o As MyClass
   
    Set o = New MyClass
   
    AddTheNumbers = o.addTwoLongs(num1, num2)
   
   
End Function


As you can see, it's a fairly simple dll.

Any ideas how to fix this?

Thanks,
HeadPygmie
headpygmieAsked:
Who is Participating?
 
Daniel Van Der WerkenConnect With a Mentor Independent ConsultantCommented:
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
The .NET assembly needs to be "findable" by the COM system. Are you registering the DLL after you build it with regasm?

http://edndoc.esri.com/arcobjects/9.2/NET/9826f81b-278c-49cc-a912-ab8d86f5e91e.htm
http://msdn.microsoft.com/en-us/library/tzat5yw6(v=vs.90).aspx

It might have to be signed. I can't recall because it's been a while since I've done interop.
0
 
headpygmieAuthor Commented:
Thanks for the reply.  Yep, it is findable.

In VBA references, I've found and checked the DLL.

In the c# assembly information for the project, I also set "Make assembly COM-Visible" to checked.

In addition, I also checked "Register for COM interop" in the build options.

I hope I didn't miss anything else.
0
 
headpygmieAuthor Commented:
Thanks!!!  I set the c# type to int and that worked.  I guess I'll to be more careful in the future.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.