TK-421
TK-421

Reputation: 349

What is the equivalent of Type Statement from VBA in C#?

What is the equivalent of Type from VBA in C#?

I am asking, because I have some code in Excel which I am trying to turn into a standalone C# application. I have a function, which comes from a .dll, which takes in a Type with some strings and longs as an argument. How would I replicate this in C#? Will this even work, or the will the function from .dll not even work in C#?

I haven't yet experimented with this, because it is all connected to a working database, and I want to know how this would work before I mess something up.

Upvotes: 2

Views: 583

Answers (3)

AAA
AAA

Reputation: 3670

The closest will be the Struct datatype. It is similar in that it is a user-defined data type containing one or more elements (members) of various data types, and these elements are accessed as fields of the Type/Struct.
Both are values types and use stack allocation (usually).

Upvotes: 1

John Wu
John Wu

Reputation: 52250

The example in the linked documentation would be rewritten as:

struct StateData 
{
    int[] CityCode;
    string County;
}

var Washington = new StateData[100];

Although at some point you'd have to initialize CityCode. In VBA you could do it implicitly but in c# you'd have to write something like:

foreach (var w in Washington) w.CityCode = new int[100];

Upvotes: 2

Heinzi
Heinzi

Reputation: 172270

You say that you use a Type in VBA to pass data to a function contained in some DLL. Seeing some code would help, but this technique is usually used to call a (Windows API or third-party) DLL expecting some C-type struct.

In C#, you would use P/Invoke to call functions in unmanaged DLLs, and you would typically use a struct for complex types required by the DLL. Using a class is also possible, but there are some subtleties to be aware of.

A simple example can be found in this SO answer:

If your DLL is part of the Windows API, you can use http://pinvoke.net/ to find ready-to-use C# declarations for the functions as well as the data structures.

Upvotes: 2

Related Questions