In this post we will have a look on how dot net supports cross language inheritance.
In some of the books i have read that dot net has a cross language inheritance support and when i was a beginner i was not really aware about this term so now after doing some research i came to know what does the term means.
Cross language means you can create or download a library in one language and use it in some other language one such example is the dot net zip library which is used for manipulating the zip files and can be used easily with any .net language but initially the dot net zip library is coded in some other language. And if the library or your code contains some error then in that case the error would be displayed or handled via try and catch block with no problem at all.
So in a nutshell a developer can make a library with any programming language of his choice and give it to his fellow developers and those guys can use this library with some other language with no problem at all not only this you can inherit the classes used in those libraries or even override some virtual methods inside that library this is the beauty of cross language inheritance and to implement this you need a reference to a component means a dll.
So in a nutshell a developer can make a library with any programming language of his choice and give it to his fellow developers and those guys can use this library with some other language with no problem at all not only this you can inherit the classes used in those libraries or even override some virtual methods inside that library this is the beauty of cross language inheritance and to implement this you need a reference to a component means a dll.
Example: For this post i will create a simple library in C# and use the reference of this library in the VB.NET code. First of all i will create a library to add two numbers using c sharp so in order to do this just follow the given steps:
Step 1: Type devenv in run and open dot net environment and select c sharp as the language and create a new project and in that select the class library just like the image given below
Now save the library with an appropriate name, The code given below is of the c sharp library that i have created and it contains a simple calculator class which has the add function which will accept two number from the user and will display the final result.
{
public class Calculator
{
public void add()
{
int a, b;
Console.WriteLine("Enter the first number");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number");
b = Convert.ToInt32(Console.ReadLine());
int result = a + b;
Console.WriteLine("The addition of " + a.ToString() + " and " + b.ToString() + " is " + result.ToString());
}
}
}
Step 2: Alright now we are done with the c sharp library part now its time to use this c sharp library in a vb.net project. so lets create a new vb.net project and add this c sharp library class reference to this vb.net project.
So create a vb.net console application and add the reference of the c sharp library into it here's a pic to guide you.
Now when you select add reference then you will see a pop up window which will ask you to browse for the library reference that you have created, just find the C sharp library project that you created and then traverse to the bin ->Debug->DotNetLibrary_Calculator.dll file and add this file.
Note: Here DotNetLibrary_Calculator.dll is the name of my reference library so don't get confused by that, you can see your own dll in the debug folder.
Step 3: Import this dll reference into our project and the below pic will show you that i have imported the reference and also created the object of the class calculator that was present in the c sharp library now i want you to build and run the project to see the final output.
When you run this project the final output will be somewhat like the image given below
You can even create a new class in vb.net inherit the calculator class (this is cross language inheritance that we are talking about) and use the functions of the calculator class in that class, here's a snap of that code
The output will be same as above.
i Hope that this post has helped you in understanding the concept of cross language inheritance, any comments positive or negative are always welcome because they will help me in my blogging capabilities and will help me to help you.
Very useful information,, thanks
ReplyDeleteweb designing
nice topic ,,helpfull
ReplyDeleteThis is my blog check out this