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

Re: Calling a VB.NET DLL from C# by Joseph

Joseph
Thu Mar 13 14:24:44 CDT 2008

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.

--
Joseph Daigle

"Jerry West" <jw@comcast.net> wrote in message
news:d4mdnRtpmK9u5kTanZ2dnUVZ_q-jnZ2d@giganews.com...
>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
>



Re: Calling a VB.NET DLL from C# by Jerry

Jerry
Thu Mar 13 14:48:18 CDT 2008

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


"Joseph Daigle" <joseph[nospam]@cridion.com> wrote in message
news:ODBDz$ThIHA.6092@TK2MSFTNGP06.phx.gbl...
> 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.
>
> --
> Joseph Daigle
>
> "Jerry West" <jw@comcast.net> wrote in message
> news:d4mdnRtpmK9u5kTanZ2dnUVZ_q-jnZ2d@giganews.com...
>>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
>>
>
>



Re: Calling a VB.NET DLL from C# by Jon

Jon
Thu Mar 13 14:56:01 CDT 2008

Jerry West <jw@comcast.net> wrote:
> 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.

--
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

Re: Calling a VB.NET DLL from C# by Jerry

Jerry
Thu Mar 13 15:04:22 CDT 2008

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

JW

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.2243972f34540a18b0c@msnews.microsoft.com...
> Jerry West <jw@comcast.net> wrote:
>> 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.
>
> --
> 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



Re: Calling a VB.NET DLL from C# by Jon

Jon
Thu Mar 13 15:21:59 CDT 2008

Jerry West <jw@comcast.net> wrote:
> 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.

--
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

Re: Calling a VB.NET DLL from C# by Martin

Martin
Fri Mar 14 09:04:07 CDT 2008

On Mar 13, 8:21 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Jerry West <j...@comcast.net> wrote:
> > 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.

Re: Calling a VB.NET DLL from C# by Jerry

Jerry
Fri Mar 14 11:59:22 CDT 2008

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

"Martin Bonner" <martinfrompi@yahoo.co.uk> wrote in message
news:849accc0-82d6-4193-90ba-60a20a2ea005@d21g2000prf.googlegroups.com...
> On Mar 13, 8:21 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
>> Jerry West <j...@comcast.net> wrote:
>> > 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.



Re: Calling a VB.NET DLL from C# by Jon

Jon
Fri Mar 14 14:27:36 CDT 2008

Jerry West <jw@comcast.net> wrote:
> 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.

--
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

Re: Calling a VB.NET DLL from C# by Jerry

Jerry
Fri Mar 14 15:19:57 CDT 2008

{
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 [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.2244e203c1d2250ab26@msnews.microsoft.com...
> Jerry West <jw@comcast.net> wrote:
>> 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.
>
> --
> 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



Re: Calling a VB.NET DLL from C# by Jon

Jon
Fri Mar 14 15:35:31 CDT 2008

Jerry West <jw@comcast.net> wrote:
> {
> 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();

--
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

Re: Calling a VB.NET DLL from C# by Jerry

Jerry
Fri Mar 14 15:49:07 CDT 2008

Thanks!

JW

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.2244f1d4bc4fe330b2a@msnews.microsoft.com...
> Jerry West <jw@comcast.net> wrote:
>> {
>> 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();
>
> --
> 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