Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
It is common nowadays to create a separate DLL that contains shared logic for applications. That way you have a centralized assembly that contains the compiled code. You also have one project where you can write and debug that shared code. In .NET, you create a class library to accomplish this. When you compile that class library, it generates a DLL. I'm inside Visual Studio, and I've opened this Solution called CreateClassLibrary. There are two projects already in here. There is a Visual Basic console application, and there is a WPF application that's written in C#.
Now I'm going to add a shared library. I'm going to go to the solution, right- click and choose to add a new project. I'm then going to choose this Class Library Template and then type in the name TextLibrary for the Name, and then click OK. Now, I'm going to compile the application to show you what happens. When I build the solution, it's going to compile all three projects. And if I right-click on the TextLibrary and choose Open Folder in Windows Explorer, you'll see that if I drill down into the bin folder and then the Debug folder, that it's creating this DLL.
Now, I'm going to come back to this folder in a minute. Let me switch to Visual Studio. I'm going to generate a class called LoremGenerator, which generates random sentences of Latin words. So, I think I should rename this Class file. So I'll come over here to my project and type in "LoremGenerator", and then press Enter. Visual Studio then prompts me and asks me if I would also like to rename the class inside that file.
That seems like a great idea to me. So I'm going to click Yes. Now you see that it has this new LoremGenerator class. I'm not going to write all the code today live. I do have the code up here in this Assets folder. There is a TXT file in here, which I'm going to double-click on. Then I'm going to copy all of the code in this class. Back to my LoremGenerator.cs file. Then I'll replace these three lines of code, Ctrl+V. I won't go through all of the code, but let me just point out that there are two methods I'm going to use: the GetWords method where I pass the numberOfWords in, and it gives me back say eight words, and a GetSentence, which has the same parameter, but this time of course it's going to put a period on the end of that sentence.
Now I'm ready to use this. This is a C# library. I'm going to go to my Visual Basic Application and add a reference to this library. I'm going to right-click, and I'm going to choose to add a reference, and I'm going to click the Browse button, and then I'm going to go back to Windows Explorer and copy this path to that Debug folder. Copy it, Ctrl+C. Switch back here, and I'm going to paste it down here where it says File name, and then press Enter. And there is the DLL that I want to have the reference to.
So I'll choose that one and then click OK. Now, just for good measure, I'm going to recompile my application and click Yes here. So now when I compile this application, it's telling it to make sure that I can use the code that's inside that DLL. Let's use it. I'm going to double-click on Module1.vb, and then I'm going to go here, and I'm going to declare an instance of that generator, Dim g As New TextLibrary. There is my namespace. There is my class.
Now I'm going to write to the console, and I'll also read from the console. Next, I'm going to go onto line 6, and I'm typing in "g.GetSentence", and let's say I wanted eight words out of that.
Let's test this out. Debug > Start Debugging, and there is my randomly generated sentence, including the period on the end. Next, I'm going to switch over to the C# application-- that would be this one down here WpfClient--and I'm going to open the MainWindow.xaml.cs file. Then inside my constructor, I will declare the C# version of that variable, var g = new.
Well, this isn't going to work, because I forgot one step. I forgot to add the reference to the library. See, I'm getting a red squiggle here. So let me comment this code out and then go over to my References folder and show you an alternative way of writing a reference. Right-click > Add Reference. Last time I browsed out and pointed to a physical location, but Visual Studio can simplify my life by letting me do something called a project reference. I click here and then I say whenever you compile this TextLibrary, I would like to know about it and get a fresh copy of that DLL.
So, I click OK here, and then if you scroll down and look at the Reference here, you'll notice it says Copy Local equal True. So not only will it update the DLL, but it'll copy the DLL into the same folder as my application. So, when I deploy the app, I've got both files. Now that I've added that Reference, I can go back to this code that wasn't working before, uncomment it, and then go to this line. There is the TextLibrary I was looking for, and then period, and there is the LoremGenerator, and then finally, "var words = g.GetWords".
And there is my new function. So, the takeaway from this is both the Visual Basic application and the C# application are sharing the code that's inside that TextLibrary DLL. As you can see, it is very easy to add references to other libraries in Visual Studio and that the Class Library template provides a trouble-free way to make that custom library.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101561 Viewers
61 Video lessons · 88320 Viewers
71 Video lessons · 72184 Viewers
56 Video lessons · 103939 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.