How to call C# function from VB.Net?

Muhilan
Posted by in Windows Forms category on for Beginner level | Views : 28669 red flag
Rating: 4.33 out of 5  
 3 vote(s)

To Call C# function from VB.Net.


 Download source code for How to call C# function from VB.Net?

Introduction

 
In this article I will explain you about how you can access the C# function from VB.Net.


ClassLibrary(C# Coding)

.Net provides create your own class library code and build into DLL and reuse the DLL file. Here created simple class library function in C# code for return a string.

1. Start new project , from the New Project dialogue box select the class library template under C# project types
2.  Name box, type a name for your Class Library. Example here name it SampleCsharp, then click OK.


4. Then Click ok you can get namespace SampleCsharp and the class(rename the class name as Muhilsample) , it look like this



5. write your code inside the class example i have written one function which get input and display the string with some manipulation string.

6.
 public string DisplayString(string GetString)
        {
           
string str = "Hello" + GetString + " Hw are you?";
            return
str;
        }
7. The above code get the input string and add front hello and how are you in the end.
8.  Build the project, it will create a DLL look like this



9. SampleCsharp.dll created , now you can use it in another project.


Window Application(Vb.Net coding)

1. Now add new project from the window, select visual basic under other languages


2. click Project > Add Reference. You should see a dialogue box appear with a few tabs on it. Click the Browse tab. Browse to where you saved your Class Library.

3. Then locate the Bin > Debug folder from above. Your DLL will be in there:

4. Click OK .now have a look at the Solution Explorer on the right, and you can  see it appear on the list of references

5.

6. Now you can use the namespace and their classes in your vb.Net
7.
  Dim obj As New SampleCsharp.MuhilSample
MessageBox.Show(obj.DisplayString("Muhil"))



8. Create a instance for the SampleCsharp.Muhilsample(its written in C# sharp) but object created in VB.NET

9. call the DisplayString with arguments

10. write the above code in page_load event

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim obj As New SampleCsharp.MuhilSample
MessageBox.Show(obj.DisplayString("Muhil"))
End Sub

11. Build and run the application output will be



Please feel free to send your feedback. Thank you.

Page copy protected against web site content infringement by Copyscape

About the Author

Muhilan
Full Name: Muhil an
Member Level:
Member Status: Member
Member Since: 11/30/2009 2:46:25 AM
Country: India


working as an IT System Analyst,tmuhilan at gmail com

Login to vote for this post.

Comments or Responses

Posted by: Vuyiswamb on: 6/18/2010
Nice and Simple Article , keep up the Good work
Posted by: Mcadeepuraj on: 6/1/2011 | Points: 25
Nice Article, keep it up
Posted by: Theresajayne on: 4/17/2012 | Points: 25
So, if i have a VB.Net class say ClientUpdateRequest, and I want to pass it into the C# how can i do it

VB.NET
Dim csharp = new CSharpClass
csharp.myCall(ClientUpdateRequest)

C#

public void MyCall(ClientUpdateRequest request)
{
}

so how do you get that to compile as its a circular reference across projects?

Login to post response

Comment using Facebook(Author doesn't get notification)