1 / 24

Working With Objects

Working With Objects. Tonga Institute of Higher Education. Introduction. Visual Basic .NET is an object-oriented language The building block of an object-oriented language is an object. Object - A self-contained entity that contains data and procedures to manipulate the data.

cyrah
Télécharger la présentation

Working With Objects

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. Working With Objects Tonga Institute of Higher Education

  2. Introduction • Visual Basic .NET is an object-oriented language • The building block of an object-oriented language is an object. • Object - A self-contained entity that contains data and procedures to manipulate the data. • An object is like a tool that we can use to do things. • You can find a list of pre-built objects in the MSDN Documentation. (www.msdn.com)

  3. Advantages of Object Oriented Programming • Code is easier to maintain • Code is more readable • Encapsulation • Show what the object can do • This is normally what we want to know • Hide how the object does it • This can be very complicated • We often don’t care how it is done • Code is easier to reuse • A piece of code, once written, should not be thrown away. It is best to reuse the code in other programs. • Example: Millions of people use MessageBox.Show(...). But it was only written once. • Code development is more efficient • You don’t code the same thing over and over again

  4. Objects • Objects are like variables with extra functionality. • Look at MSDN documentation to find functionality. • Examples: • String • String.Replace(…) • String.PadLeft(…) • TextBox • TextBox.Text(…) • TextBox.MaxLength(…)

  5. Classes vs. Objects • Object - A self-contained entity that contains data and procedures to manipulate the data. • Class - The blue print or design for creating an object. • Instantiate – The act of creating an object from a class • Instance – An instantiated class/object

  6. Using Object Variables • 2 Steps to using variables • Declare the variable • Instantiate the variable / Initialize the variable

  7. Declaring Object Variables – 1 • Declare the variable – Tell the computer to reserve a space in memory for the variable. • You need to tell the computer 2 things: • Name of the variable • Type of the variable (What kind of variable you have) • String • StringBuilder • TextBox Type Name

  8. Declaring Object Variables – 2 • Use a name that is easy to remember. • Do not use x, y, z • Begin each separate word in a name with a capital letter • Examples • FirstName • CustomerID • This works exactly the same for primitive variables!

  9. Instantiating Object Variables / Initializing Object Variables • Initialize the variable – Assign an initial value to a variable • Instantiate – The act of creating an object from a class • Use the new keyword Sometimes extra data is required New Keyword Type of Object

  10. Declaring and Initializing Object Variables in 1 line • You can declare and initialize a variable in 1 line.

  11. Demonstration Declaring, Instantiating and Initializing Objects

  12. Methods • Methods - Pieces of code that perform a single function • Use dot notation to access it • Example: <Object name>.<method name>(<parameters>) • You can find a list of methods in the MSDN Documentation. • You can also find a list of methods using the IntelliSense capability. • Calling a Method – The act of using a method

  13. Method Input Output Method Inputs - 1 • Some methods take inputs • Parameter/Arguments – A piece of information that provides additional information to the method as to how it should behave. • Parameters should be in the parenthesis next to the method name • The order they are passed is important • Values are separated by commas • If you are not passing any parameters, you may or may not use (). It is up to you. • Example: String.Trim • Example: String.Trim()

  14. Method Input Output Method Inputs - 2 • Some methods take inputs • You can find a list of parameters in the MSDN documentation. • You can find a list of parameters using the IntelliSense capability. Click on link to get more information Method Name Parameters

  15. Method Input Output Method Outputs • Some methods return outputs • When something is returned, it may or may not be used. • The programmer chooses what to do with the data returned. • Only one thing may be returned. • If something is coming back, we can see “As <object>” at the end of the method in IntellliSense • If nothing is coming back, you will not see “As <object>” at the end of a method in IntellliSense • Use popup windows while coding to see what is being returned Output Information

  16. Functions vs. Subroutines • Subroutine – A method that does not return anything • Function – A method that returns something

  17. Demonstration Methods

  18. Constructor • Constructor – A method that is automatically executed when an object is created. • This allows you to set initial values for the object. • Many objects have multiple constructors. (They are overloaded) • You can find a list of constructors in the MSDN Documentation. • You can also find a list of constructors using the IntelliSense capability. You don’t need parenthesis if you are not passing parameters Dim x as StringBuilder = new StringBuilder Dim y as StringBuilder = new StringBuilder(“hello”) Dim z as StringBuilder = new StringBuilder(6)

  19. Demonstration Constructors

  20. Attributes / Fields / Properties • Attributes / Fields / Property – A variable that a class allows others to see • Use dot notation to access it • Example: <Object name>.<field name> • You can find a list of attributes / fields / properties in the MSDN Documentation. • You can also find a list of attributes / fields / properties using the IntelliSense capability.

  21. Demonstration Attributes / Fields

  22. Method Overloading • If two methods do the same thing, they should have the same name • Overloading - Having multiple methods with the same name but different parameters • The correct method to use is determined by matching up the number and type of arguments. • Therefore, you can’t have 2 methods with the same name and same number & type of arguments. • Without overloading, we would have to remember more function names. That would make code more complicated.

  23. Demonstration Overloaded methods

  24. Members • Member – An attribute/field or method. • Sometimes used to refer to the both as a whole.

More Related