?
Solved

VBA using C# Struct from DLL file

Posted on 2011-10-28
3
Medium Priority
?
385 Views
Last Modified: 2013-12-16
Hi,

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


Thanks,
0
Comment
Question by:mcs26
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 37044231
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.
0
 

Author Comment

by:mcs26
ID: 37044262
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


 
0
 
LVL 40
ID: 37045926
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question