1 / 27

Objects

Objects. What an object is What the term state means with respect to objects What a class is What it means to instantiate an object What properties are What methods are How to use some of the objects provided by Visual Studio .NET. What an object is.

saskia
Télécharger la présentation

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. Objects • What an object is • What the term state means with respect to objects • What a class is • What it means to instantiate an object • What properties are • What methods are • How to use some of the objects provided by Visual Studio .NET

  2. What an object is • An object is nothing but a real word entity which has • Name • state • Action • An object is just a simplification of something that you wish to use in your program

  3. What the term state means with respect to objects • The state of an object is determined by the values of the properties used to describe the object

  4. What is a class? • A class is a template used to describe an object • A class is an abstraction or simplification of some object you observe in the real world • A class has two basic components • Properties • Methods

  5. Properties and Methods • Properties: Values • Name: Ahmad • Height 170 cm • Hair Color: black • Build: Strong • Glasses: No • Clothing: casual • Shoes: black • Accessories: back Pack • Gender: male • Methods: • CarrySign() • Speak() • Walk() • WaveHands()

  6. What does it mean to instantiate an object? • Instantiating an object means to allocate memory for the object of the class • clsPerson myFriend • myFriend = new clsPerson()

  7. Free memory RAM Null 80000 (myFriend) User program Visual studio 2008 Windows operating system

  8. High Memory Free memory RAM Data 90000 90000 80000 (myFriend) User program Visual studio 2008 Low Memory Windows operating system

  9. How to access its properties? • myFriend.Name = “Issy”; • myFriend.Gender = “F”; • myFriend.Height = 59;

  10. Object syntax • objectName.property • objectName.property = value ; • myFriend.Height = “168”; • txtName.Text = myFriend.Height;

  11. Why Hide the Data Inside an Object? • You hide the data inside an object in an attempt to protect the data from accidental changes by other parts of the program. • The process of hiding data within an object is called encapsulation.

  12. Developing a Program Plan • To write good programs you must have a plan. • Central to a programming plan is an algorithm. • An algorithm is simply a step - by - step recipe, or plan, for solving a programming problem.

  13. The Five Program Steps • Step 1: Initialization. • Step 2: Input. • Step 3: Process. • Step 4: Display. • Step 5: Termination.

  14. Creating a Simple ApplicationUsing Objects • The goal of the program is to gather address information about the user and then simply redisplay that information as if it were a mailing label.

  15. Using the Program Steps to Create a Program Plan • Step 1: Initialization • This is a simple program and you don ’ t really have any fancy initialization to do. Any necessary initialization tasks are done automatically for you by Visual Studio.

  16. Step 2: Input This is the step where you must ask yourself, “What information do I need to solve the programming task at hand”? • 1. Name • 2. Street address • 3. City • 4. State • 5. Zip (or postal) code

  17. These are all the inputs we need . Now , should we hard-code the inputs or let user enter the data at run time ?

  18. Designing a Program ’ s User Interface • Follow the KISS (Keep It Simple Stupid) Principle. • Use familiar interfaces. • The user interface should have a natural flow to it.

  19. Step 3: Process: This is the step where you devise the algorithm that produces the desired result of the program. The desired result of this program simply is to display the user information in a mailing - label format. • Step 4: Display: In this program, you simply display the user ’ s name and address information.

  20. Step 5: Termination: Because you didn't write any code to do anything tricky in the initialization step, you don ’ t need to do anything in the termination step to clean up and gracefully end the program. Instead, you can simply have a button object (such as an Exit button) that the user can click to end the program.

  21. Using C# to Implement Our Program Plan • Creating a New C# Project File -> New -> Project and then select empty project. Rename it as mailingLable. • Adding Program References to Your Project Project -> Add Reference and add • 1. System • 2. System.Drawing • 3. System.Windows.Forms

  22. Adding a New Item to the Project Now you need to create a form object that is going to hold the (label and textbox) objects that you want to use in your program. Project -> Add new item menu and select code file Rename it as frmMain.cs

  23. Adding Program Code to the frmMain Object using System; using System.Windows.Forms; public class frmMain : Form { #region Windows code private void InitializeComponent() { } #endregion public frmMain()

  24. public frmMain() { InitializeComponent(); } public static void Main() { frmMain main = new frmMain(); Application.Run(main); } }

  25. Setting the Project Properties • Select Project -> MailingLabel Properties • select Windows Application from the output type choices. • set the startup object to be the object that contains the Main() method for the application I.e. frmMain.

  26. The user Interface

  27. Adding program code private void btnDisplayOutput_Click(object sender, EventArgs e) { String buffer; buffer = “Mailing Label:” + Environment.NewLine +Environment.NewLine; buffer = buffer + “ Name: “ + txtName.Text +Environment.NewLine; buffer = buffer + “Address: “ + txtAddress.Text +Environment.NewLine; buffer = buffer + “ City: “ + txtAddress.Text +“ State: “ + txtState.Text + “ Zip: “ + txtZip.Text; txtDisplayOutput.Text = buffer; }

More Related