VBA using C# Struct from DLL file


I have created a simple C# Class that I am using in Excel & Access. The question I have is when I look in the object browser in either Access or Excel I can see the name of my class and its functions. I also have a struct defined in my class & in the object browser the stuct appears as its own class not as a member of 'MyClass' why is this the case? I have moved the struct decelartion out of 'MyClass' and put under the namespace but this does not seem to change anything.

namespace MyNameSpace

	class MyClass

		public struct MyStruct
			int VariableOne;
			int VariableTwo;

		int MyInt;
		public void MyFunctionOne()


		public void MyFunctionTwo()




Open in new window

Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
It has to do with what Structure and Class means in VBA.

Structures do not exist as such in VBA. There is something called a Type that looks like your C# structure, but contrary to what you can do in structure, a Type can contain only a set of variable. No method.

In .NET, a structures can contain a constructor and methods. This is almost the equivalent to a VBA classes, that are very simple things. In order to be able to use all the features of a .NET structure, VBA must handle it as if is one of its own classes.

Even if you do not have methods in your own structure, the mechanism that lets you see a structure in VBA is the same for any structure. It is mapped to a class instead of a Type, that would be a better match with your simple structure.
mcs26Author Commented:
Hi James,

Thanks for the reply and the explanation. So am I correct in saying that VBA will alway map a .Net Struct to a Class and that in VBA to use this 'class' you do not have to initalise it like you would for a normal class, i.e. below?

Dim objClass as MyProject.MyClass
set objClass = new MyProject.MyClass

Dim objStruct as MyProject.Struct
objStruct.VariableOne = 5

Open in new window

Jacques Bourgeois (James Burger)PresidentCommented:
I cannot be sure that it will be allways map it that way. I dropped VB6 and VBA quite fast after VB.NET came to be, so I did not have a lot of experience referencing .NET from VBA. I also do not use structures very often in my own classes.

But I am quite sure it will. It did in the 2 or 3 instances where I had to do it back around 2002, while I was moving my code from VB classic to VB.NET.

The explanations I gave you is not official, but I am also quite sure it is right. I would bet my 22 years experience in object programming, the 12 years I worked mostly in VB6 and VBA (still do a little VBA in Office) and my 12 years in .NET on the fact that the reasons I give are close to what really happens under the cover.

A .NET structure having almost all the features of a VBA class (only the Terminate event is not there), and even a few extras (constructors with parameters, overloading of methods to name just two), it makes sense that the VBA environment sees it as a class.

I can also ad that since the notion of a namespace did not exist in VBA, the concept of referencing a type declared inside of another type does not exist either in VBA, so the object browser and IntelliSence are not able to display a structure inside of a class.
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.