Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Calling a VB.NET DLL from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jerry West - 13 Mar 2008 20:09 GMT
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
Joseph Daigle - 13 Mar 2008 20:24 GMT
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.

Signature

Joseph Daigle

>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
[quoted text clipped - 21 lines]
>
> JW
Jerry West - 13 Mar 2008 20:48 GMT
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

> 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
[quoted text clipped - 27 lines]
>>
>> JW
Jon Skeet [C# MVP] - 13 Mar 2008 20:56 GMT
> In C#, the name of the DLL I added a ref to is: TestDLL
>
[quoted text clipped - 7 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jerry West - 13 Mar 2008 21:04 GMT
Are you indicating that I should recompile the DLL in VB.NET with Public AND
Shared properties?

JW

>> In C#, the name of the DLL I added a ref to is: TestDLL
>>
[quoted text clipped - 13 lines]
> If it's Public but not Shared, you need an instance of the class - you
> can only call instance methods on instances.
Jon Skeet [C# MVP] - 13 Mar 2008 21:21 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Martin Bonner - 14 Mar 2008 15:04 GMT
> > 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.

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.
Jerry West - 14 Mar 2008 17:59 GMT
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

>> > Are you indicating that I should recompile the DLL in VB.NET with
>> > Public AND
[quoted text clipped - 27 lines]
> you switch it off, it will suggest using TestDll as the name of the
> namespace, and Add as the name of the function.
Jon Skeet [C# MVP] - 14 Mar 2008 20:27 GMT
> Thanks for that --and the proper syntax. Just what I was needing given my
> lack of C# knowledge.
[quoted text clipped - 20 lines]
>
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jerry West - 14 Mar 2008 21:19 GMT
{
           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

>> Thanks for that --and the proper syntax. Just what I was needing given my
>> lack of C# knowledge.
[quoted text clipped - 31 lines]
> See http://www.pobox.com/~skeet/csharp/complete.html for details of
> what I mean by that.
Jon Skeet [C# MVP] - 14 Mar 2008 21:35 GMT
>         {
>             int x;
[quoted text clipped - 7 lines]
>
> "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();

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jerry West - 14 Mar 2008 21:49 GMT
Thanks!

JW

>>         {
>>             int x;
[quoted text clipped - 13 lines]
>
> TestDLL.Class1 testObject = new TestDLL.Class1();

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.