1 / 62

Creating Classes

Creating Classes. Tonga Institute of Higher Education. Current Status. The form class contains all of our code Code is added to the form class Code may use variables, methods and events Code may use other objects When a program is run, the form object is used. Custom Classes and Objects.

pahana
Download Presentation

Creating Classes

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. Creating Classes Tonga Institute of Higher Education

  2. Current Status • The form class contains all of our code • Code is added to the form class • Code may use variables, methods and events • Code may use other objects • When a program is run, the form object is used

  3. Custom Classes and Objects • Sometimes, the objects provided by Microsoft do not fit our needs. • We can define custom objects. • We can use custom objects just like normal objects

  4. How to Define a Class Access Specifier Class Name • Access Specifier - Covered later • Class Name • Class names should be nouns • One word • The first letter of each internal word is capitalized. • Keep your class names simple and descriptive. • Example: • Customer • SalesOrder

  5. Demonstration Defining a class

  6. How to Define a Variable Access Specifier Variable Name Type • Access Specifier – Covered later • Variable Name • One word • The first letter of each internal word is capitalized. • Keep your variable names simple and descriptive. • Example: • FirstName • PhoneNumber • The type can be a primitive or an object • Example: Integer, String, Boolean, Student

  7. Demonstration Defining a variable

  8. How to Define a Method(Subroutines and Functions) • Methods - Pieces of code that perform a single function • Subroutine – A method that does not return anything • Function – A method that returns something

  9. Difference Between Subroutines and Functions Access Specifier Parameter Type Parameter Name Method Name Access Specifier Parameter Type Method Name Parameter Name Return Type Return Data

  10. Subroutines and Function Names • Verbs • One word • The first letter of each internal word is capitalized. • Keep your method names simple and descriptive. • Example: • RunFast • GetBackground • ShowWelcomeMessage

  11. Subroutines and Function Parameters • Parameters are optional • Each parameter that your method accepts must have a name and type • The name should be: • One word • The first letter of each internal word is capitalized. • Keep your parameter names simple and descriptive. • The type can be a primitive or an object • Example: String, Integer, Customer • Example: • Private Sub ShowSummary(ByVal StudentID As Integer) • If multiple parameters are used, separate them with commas • Example: Private Sub ShowSummary(ByVal StudentID As Integer, ByVal StudentName as String) • Use the name inside the method parameter list to access the parameter in your code • StudentID • StudentName

  12. Function Return Types Access Specifier Parameter Type Method Name Parameter Name Return Type Return Data • Return Type – Sets the type of data that will be returned from the function. • The type of data can be a primitive or an object • Example: String, Integer, Customer • If you are not returning anything, do not use a function. Use a subroutine. • Do not forget to use the Return keyword in your function

  13. Demonstration Defining a subroutine

  14. Demonstration Defining a function

  15. Method Overloading • Overloading - Having multiple methods with the same name but different parameters • To make an overloaded method, create a method with the same name but different parameters. • Return types for functions can be overloaded but it is not a good idea Same Name Different Parameters

  16. Demonstration Method Overloading

  17. Properties and Code Conventions • Generally, make all class variables private • Use properties to access your variables • Properties – a specialized method used to get and set a class’ variables • This could be done using regular methods, but properties are used more often

  18. Regular Methods vs. Properties Same Thing!

  19. Property Features • Read-Only Properties • Write-Only Properties

  20. Demonstration Properties

  21. Using the custom class • Use custom objects just like normal objects • Declare and instantiate them • Use methods and properties • A class cannot use itself • A different class must use a class • Example: A StudentSystem class would use a Student class

  22. Demonstration Using the custom class

  23. Dynamic Link Libraries • Until now, all of our VB.Net programs have been .exe files • This allows us to run a program • Sometimes, we need to share a class that we’ve made • A Dynamic Link Library is a file that contains classes for use by other programs. • They end with .dll • Example: System.dll

  24. Seeing DLLs in Action • When a form is run, many different classes are needed. Example: Form class, textbox class, and more. • VS.Net loads dll files when a form is run. The dll files contain the class data for the objects needed to run the program • The loading of dll files can be seen in the Output box

  25. Demonstration Seeing DLLs in Action

  26. Creating a DLL File • We can create Dynamic Link Libraries • Right click on the project in the Solution Explorer • Select Properties • Select the Class Library Output Type • Click OK button • Compile the project. When the project is compiled, VS.Net creates a .dll file in the Bin directory

  27. Demonstration Creating a DLL File

  28. References • Many projects use dll files • Many dll files used are listed in the references folder

  29. Demonstration References

  30. Adding DLL Files to a Project - 1 • To use a dll file in a project, right click on the References folder and select Add Reference • The Add Reference form is displayed

  31. Adding DLL Files to a Project - 2 • Use the .Net tab to add a .Net dll • Use the COM tab to add a COM dll. (These are dll files made before VB.Net) • Use the Projects tab to add a reference from a project in the same solution

  32. Demonstration Adding DLL Files to a Project

  33. Using DLL Files • To use a class defined in a DLL file • Add a reference to the dll file to the project • Write the fully qualified namespace to access the object Dim x as new ClassLibrary1.Student Remember: Namespaces are not always the same as the file name! Class Name Fully Qualified Namespace

  34. Demonstration Using DLL Files

  35. Import Statements • Import Statements allow you to not always type the fully qualified namespace Imports ClassLibrary1.Student • There are import statements defined at the project level. To see these, go to Project Properties -> Common Properties -> Imports

  36. Demonstration Import Statements

  37. .DLL and .EXE File Tips • A .Dll file cannot be run. Rather, it is built • Do not use the same name for a .dll and exe file • If the location of a .dll file reference is changed, VS .Net will show a yellow error message. To fix this, remove the incorrect reference and add a new reference • When you make a change in a .dll file, the project must be rebuilt. • When a .dll file is changed in a separate solution from the .exe file, the .exe solution must be reloaded • An .exe project that uses a .dll file will have a copy of the .dll file saved to the bin directory. When giving the program to other people, both the .dll and .exe files must be given to run properly.

  38. Demonstration .DLL and .EXE File Tips

  39. What is Scope? • The scope of an element (variable, class, method, etc.) defines what parts of a program can see it Using Public instead of Private changes the scope of this variable.

  40. Levels of Scope • Block • Procedure • Class • And others

  41. Block Scope • A block is a set of statements terminated by an End, Else, Loop, or Next statement • Example: The code within a For...Next or If...Then...Else...End. • An element declared within a block can be used only inside that block • Use Dim to declare elements with block scope • The Static keyword may also be used (Covered later) Block

  42. Demonstration Block Scope

  43. Procedure Scope Procedure • A procedure is a subroutine or function • An element declared within a procedure is only usable in the procedure. • Elements at this level are also known as local elements. • Use Dim to declare elements with procedure scope • The Static keyword may also be used (Covered later) • Works like block scope but you must also consider the parameters Procedure

  44. Demonstration Procedure Scope

  45. Class Scope • Declare elements at this level by placing the declaration statement outside of any procedure or block within the class. • These access specifiers can be used • Public • Private • Dim • Friend • Protected • Protected Friend

  46. Access Specifiers Access Specifier • Public • Can be used by everything • Private • Can only be used by code inside the same class • Dim • Same as Private • Friend • Can be used by code inside the same project Name Dim FirstName as String Type • Protected • Can be used by code that inherits from this class • Protected Friend • Combination of Protected and Friend

  47. Demonstration Access Specifiers

  48. Scope:Global Variables vs. Local Variables • Global Variables • Variables defined outside of a procedure or block but inside of a class • Also called member variables • Can be readonly by using the ReadOnly key word • Local Variables • Variables defined inside procedures or blocks • They are only visible by code inside the procedure or block • When looking for a value, start local. Go global if you can’t find the variable

  49. Demonstration Global vs. Local Variables

  50. Element Lifetimes • The lifetime of a declared element is the period of time during which it is available for use. • Local variables declared with Dim exist only while the procedure in which they are declared is executing. • A variable declared in a class exists as a separate copy for each instance of the class in which it is declared; each such variable has the same lifetime as its instance.

More Related