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.