Object properties, methods and events make up what is known as the objects interface. this interface may be considered the private view of the object and it describes how that object can be manipulated. Objects also have a private view: the data and code that the object does not expose to public view, but retains in order to execute its functions. When you invoke the clear method of a List Box control, for example, you do not know how the list box clears its contents. It just does it and you know that it works. To illustrate encapsulation, let’s conduct a brief and simple exercise.
- Start-->All Programs -->Microsoft Visual Basic 5.0-->Visual Basic 5.0
- Select Standard EXE Project from the Project Type Window.
- The Project Window is displayed with a Blank Form (Form1).
- Select the Text Box Tool from the Toolbox and draw a Text Box on Form1.
- Draw Another Text Box below the first Text Box.
- Select Start from the Run Menu.
- Set the insertion point in the upper text box and type Microsoft Visual Basic.
- Select the text you have written using your mouse (or hold the Shift key down and use the directional keys).
- Copy the selected text onto the Windows Clipboard (Ctrl+C).
- Using the Tab key, shift to the lower text box and paste the text (Ctrl+V) from the Clipboard into the lower text box.
- Using the Backspace key, delete part of the text that you pasted.
- You can maximize the Form to fit in the Application Window or minimize it to remain as an icon on the task bar.
- Click on the Close Button of the Form at the right top corner to stop running the program.
Consider all the functionality contained within this application without a single line of Visual Basic code being written. Three windows, a form, and two text boxes, were created. One of these windows, the form, uses the default colors specified in the Windows Control Panel. This form can be resized, minimized into an icon, or maximized to fill the entire screen. It can be moved around the screen with the mouse.
The two text boxes allow the user to erase existing text and type text. Text could be copied and pasted. The text controls even indicate the user’s location by displaying a cursor.
How does the Text Box control manage the insertion point? How does it "know" to erase text when the Backspace key is pressed? You don't know. That's the private view of the Text Box object. You don't really have to know the inner workings of the Text Box object. It works, and you can use it to build your own applications. That's the power of encapsulation!
No comments:
Post a Comment