1 / 15

Class and DLL

Class and DLL. In a C# program, we can collect all functions in another class and write the new class in a separate file. Then we can create the object of this class and call the functions. If the function is defined as static , then we can directly call this function from class name.

luigi
Download Presentation

Class and DLL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class and DLL

  2. In a C# program, we can collect all functions in another class and write the new class in a separate file. Then we can create the object of this class and call the functions. If the function is defined as static, then we can directly call this function from class name. However, any function to be called must be public . Add function to a class

  3. Step1. Go to Project Add Class 2 1 Step2. Fill class name which is the same as file name 3

  4. Now we got the empty class as following usingSystem; namespace ConsoleApplication1 { publicclass util { public util() { } } }

  5. Then we Copy functions into class block usingSystem; namespace ConsoleApplication1 { publicclass util { public util() { } public static int[] GetIntegers(){ . . . . . } public static int GetInteger() { . . . . . .} public static string Reverse(string str) { . . . . . . } } } Now it is ready for use

  6. Use Dynamic Linked Library Dynamic linked library is a component that wraps class. Any Dynamic linked library file has extension *.dll. To make dll Step1: Go to File -> New -> Project… 1

  7. 3 2 4 Choose C#  Choose Class Library  Fill DLL name  Click OK

  8. Now we got the empty class Library as following usingSystem; namespaceDLLProject { publicclassClass1 { publicClass1() { } } }

  9. Then we Copy Util class (over write Class1) usingSystem; namespaceDLLProject { publicclass util { publicutil() { } public static int[] GetIntegers(){ . . . . . } public static int GetInteger() { . . . . . .} public static string Reverse(string str) { . . . . . . } } } Now we click build to build dll

  10. After build solution, We can see DLLProject.dll in debug folder

  11. Use DLL for program 1 Click Add Reference… 2 Click Browse…

  12. 3 Click DLLProject.dll and Click Open

  13. 4 Then click OK

  14. We can see DLLProject in reference 5 6 Add using DLLProject at the beginning

  15. Write output to File Setup code FileStream fs = newFileStream("out.txt", FileMode.Create, FileAccess.Write); StreamWriter writer = newStreamWriter(fs); Then we can call Writer.Write(. . .); Writer.WriteLine(. . .); In the end, we must call Writer.Close(); Fs.Close();

More Related