Calling a VB.NET DLL from C#

J

Jerry West

I have created a simple VB.NET DLL. I want to test it using C# by calling
its only function and getting back the result. The function within the DLL
is:

Public Function ADD(ByVal first As Integer, ByVal sec As Integer)

Dim abc As Integer

abc = first + sec

Return abc

End Function

I compiled the DLL and then opened up VS2008 and started a C# project. I
added a reference to the DLL.

I know nothing about C#. My question is what else do I need to do in order
to be able to call this function, this DLL, from C#. Do I need to add a
"using" statement? What code statement would I use to call the function? Do
I need to declare the function within the DLL somewhere? If so, how?

Thanks for any insight!

JW
 
J

Joseph Daigle

You don't need to do anything specific to handle methods compiled from other
..NET languages. For C#, you can use the "using" statement to quickly access
types in whatever namespace your compiled code lives in, or write out the
full namespace/type as your call the code.
 
J

Jerry West

Thanks....

In C#, the name of the DLL I added a ref to is: TestDLL

I typed the following in:

i = TestDLL

I was pleased to see that Intellisense had the name of the DLL as a drop
down. I selected it. The next drop down selection was the name of the class
the function was created in. OK, but then there never was an option for the
function itself. Shouldn't there be? How else would I call it? The function
was created in a Public Class and the function is Public.

What am I missing? I made no "using" statement additions. Should I have?

Thanks for any help!

JW
 
J

Jon Skeet [C# MVP]

Jerry West said:
In C#, the name of the DLL I added a ref to is: TestDLL

I typed the following in:

i = TestDLL

I was pleased to see that Intellisense had the name of the DLL as a drop
down. I selected it. The next drop down selection was the name of the class
the function was created in. OK, but then there never was an option for the
function itself. Shouldn't there be? How else would I call it? The function
was created in a Public Class and the function is Public.

If it's Public but not Shared, you need an instance of the class - you
can only call instance methods on instances.
 
J

Jerry West

Are you indicating that I should recompile the DLL in VB.NET with Public AND
Shared properties?

JW
 
J

Jon Skeet [C# MVP]

Jerry West said:
Are you indicating that I should recompile the DLL in VB.NET with Public AND
Shared properties?

Well, it's probably simpler to create an instance of the class and call
the method that way.
 
M

Martin Bonner

Well, it's probably simpler to create an instance of the class and call
the method that way.

Simpler, but not necessarily more correct. It really depends on
whether the eventual method needs to access instance data or not (I
assume the OP currently has a "Hello World" style function).

To the op:

If you need an instance, you would write something like:

TestDLL.TestClass testObject = new TestDLL.TestClass();
i = testObject.ADD(1,2);

if you make the function Shared (which in C# is spelt "static"), you
write
i = TestDLL.TestClass.ADD(1,2);

in both examples, you can get rid of the "TestDLL." if you put:
using TestDLL;
at the top of the file (I wouldn't though - I had the way that lots of
"using" statements make it impossible to work out where the classes
are coming from).

I would also recommend using FxCop to keep your code clean. Unless
you switch it off, it will suggest using TestDll as the name of the
namespace, and Add as the name of the function.
 
J

Jerry West

Thanks for that --and the proper syntax. Just what I was needing given my
lack of C# knowledge.

Just one final item. I wanted to actually test the call. However, I need to
declare i as shown in the example:

i = TestDLL.TestClass.ADD(1,2);

I did so like:

int i;

But I'm still, apparently, missing something. VS flags i in this statement:

i = TestDLL.TestClass.ADD(1,2);

It indicates "A new expression requires (), [], or {} after type". I tried
all three but each time resulted in more errors, i.e.:

i() = TestDLL.TestClass.ADD(1,2);
i[] = TestDLL.TestClass.ADD(1,2);
i{} = TestDLL.TestClass.ADD(1,2);

I tried it with my declaration as well. What am I missing?

JW
 
J

Jon Skeet [C# MVP]

Jerry West said:
Thanks for that --and the proper syntax. Just what I was needing given my
lack of C# knowledge.

Just one final item. I wanted to actually test the call. However, I need to
declare i as shown in the example:

i = TestDLL.TestClass.ADD(1,2);

I did so like:

int i;

But I'm still, apparently, missing something. VS flags i in this statement:

i = TestDLL.TestClass.ADD(1,2);

It indicates "A new expression requires (), [], or {} after type". I tried
all three but each time resulted in more errors, i.e.:

i() = TestDLL.TestClass.ADD(1,2);
i[] = TestDLL.TestClass.ADD(1,2);
i{} = TestDLL.TestClass.ADD(1,2);

I tried it with my declaration as well. What am I missing?

Well, we've got no idea what the context of your statement is.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jerry West

{
int x;
InitializeComponent();
TestDLL.Class1 testObject = new TestDLL.Class1
x = testObject.ADD(1,2);
}

The IDE warns me --by placing a line underneath the "x" var where I am
making the assignment-- it states:

"A new expression requires (), [], or {} after type".

Of course, I cannot setup thru the code due to this error. I'm sure its
something simple but being new to C# and finding examples on the Net showing
what I already am doing I just don't see the problem.

JW


Jon Skeet said:
Jerry West said:
Thanks for that --and the proper syntax. Just what I was needing given my
lack of C# knowledge.

Just one final item. I wanted to actually test the call. However, I need
to
declare i as shown in the example:

i = TestDLL.TestClass.ADD(1,2);

I did so like:

int i;

But I'm still, apparently, missing something. VS flags i in this
statement:

i = TestDLL.TestClass.ADD(1,2);

It indicates "A new expression requires (), [], or {} after type". I
tried
all three but each time resulted in more errors, i.e.:

i() = TestDLL.TestClass.ADD(1,2);
i[] = TestDLL.TestClass.ADD(1,2);
i{} = TestDLL.TestClass.ADD(1,2);

I tried it with my declaration as well. What am I missing?

Well, we've got no idea what the context of your statement is.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jon Skeet [C# MVP]

Jerry West said:
{
int x;
InitializeComponent();
TestDLL.Class1 testObject = new TestDLL.Class1
x = testObject.ADD(1,2);
}

The IDE warns me --by placing a line underneath the "x" var where I am
making the assignment-- it states:

"A new expression requires (), [], or {} after type".

That's because you haven't got () or a semi-colon on the line above.

It should be:

TestDLL.Class1 testObject = new TestDLL.Class1();
 
J

Jerry West

Thanks!

JW

Jon Skeet said:
Jerry West said:
{
int x;
InitializeComponent();
TestDLL.Class1 testObject = new TestDLL.Class1
x = testObject.ADD(1,2);
}

The IDE warns me --by placing a line underneath the "x" var where I am
making the assignment-- it states:

"A new expression requires (), [], or {} after type".

That's because you haven't got () or a semi-colon on the line above.

It should be:

TestDLL.Class1 testObject = new TestDLL.Class1();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top