I have a class project a.dll which is compiled in C#. It contains the following code.
public class MyClass{
public int Add(int i,int j)
{
return (i+j);
}
public int add(int i,int j)
{
return (i+j)*2;
}
}
from a C# project I can call these functions like this
public class MyOtherClass{
MyClass mcls=new MyClass();
mcls.Add(1,2);
mcls.add(2.3);
}
But how can I call this from a VB.Net Project
? I am not in a position to use Visual Studio right now. So its very helpful if someone will provide an answer.
EDIT 1
I am having a VB.NET project and I need to add the reference of a C# dll (say dll contains MyClass
).So that I can call two methods (Add(int,int) , add(int,int)
). But in VB.NET this is case sensitive. Is there any way to achieve this ?
EDIT 2
Suppose I added reference to the dll and so I can able to call the functions.
Dim myObj as New MyClass
myObj.Add(1,2)
myObj.add(1,2)
If this code works how the compiler identify the correct function ?
When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
No you can't have a nested function in C . The closest you can come is to declare a function inside the definition of another function. The definition of that function has to appear outside of any other function body, though.
In C, if a function is called before its declaration, the compiler assumes the return type of the function as int. For example, the following program fails in the compilation.
A function is a block of code that performs a specific task. C allows you to define functions according to your need. These functions are known as user-defined functions. For example: Suppose, you need to create a circle and color it depending upon the radius and color.
Your best bet is to use reflection here - VB simply cannot determine which function you are calling since in VB 'add' is identical to 'Add'.
Here's what I did to test it (I'm not really sure which 'BindingFlags' you need to combine here):
Dim mcls As New [MyClass]
Dim t As Type = mcls.GetType()
Dim x = t.InvokeMember("add", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, mcls, New Object() {1, 2})
Dim y = t.InvokeMember("Add", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, mcls, New Object() {1, 2})
if your C# code is CLS compliant, you can simply add a reference to it to your vb.net project.namespace , public members in the DLL can be acessed
More here
http://www.christec.co.nz/blog/archives/290
You can use free online convesion tools also .
eg : http://www.developerfusion.com/tools/convert/vb-to-csharp/
update:
Read this http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/dda8d7cb-0fa1-43d6-a90f-6c4bed0b40bb
Dim c As New MyClass()
MsgBox(c.Add(1, 2)) 'if only Add() is Available
update2:
*As per above link Note:*
Also, in the C# project, add the following to AssemblyInfo.cs:
using System;
// etc
[assembly: CLSCompliant(true)]
update3:
as i said above you want to ensure that you C# code is CLS compliant.
C# is case-sensitive, where VB.NET is not. you are violating CLS guidelines
Please Note update2 to ensure compliant nature
At last i found the article that guided me long ago
http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/
update4:
Another Scope http://msdn.microsoft.com/en-us/magazine/cc163750.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With