Lesson 5 PP

Download Lesson 5 PP

Post on 28-Jun-2015




0 download

Embed Size (px)


<ul><li> 1. Microsoft Visual Basic 2005 BASICS Lesson 5 Data Types and Variables</li></ul> <p> 2. Objectives </p> <ul><li>Describe the purpose of data types and variables. </li></ul> <ul><li>Use the AutoSize property. </li></ul> <ul><li>Declare and use variables. </li></ul> <ul><li>Describe the scope of variables. </li></ul> <ul><li>Describe the Object data type. </li></ul> <p> 3. Data Types </p> <ul><li>Computers are all about data. </li></ul> <ul><li>Visual Basic supports a certain set of data types. </li></ul> <ul><li>You can choose to store data in memory locations called variables. </li></ul> <ul><li>Variables </li></ul> <ul><li><ul><li>Can be used to store and manipulate all kinds of data </li></ul></li></ul> <p> 4. Data Types (cont.) 5. Using the AutoSize Property </p> <ul><li>The AutoSize property </li></ul> <ul><li><ul><li>Adjusts the size of a control to fit its contents </li></ul></li></ul> <ul><li>Label control example </li></ul> <ul><li><ul><li>The AutoSize property will shrink or enlarge the label to fit the text. </li></ul></li></ul> <p> 6. Using the AutoSize Property (cont.) 7. Using the AutoSize Property (cont.) 8. Declaring Variables </p> <ul><li>Using a variable in your programs </li></ul> <ul><li><ul><li>Let the compiler know</li></ul></li></ul> <ul><li><ul><li><ul><li>That you want to set up a memory location as a variable </li></ul></li></ul></li></ul> <ul><li><ul><li><ul><li>What you want to call the variable </li></ul></li></ul></li></ul> <ul><li><ul><li><ul><li>What data type you want the variable to have </li></ul></li></ul></li></ul> <ul><li>Declare a variable </li></ul> <ul><li><ul><li>Use theDimstatement </li></ul></li></ul> <p> 9. Rules for Naming Variables </p> <ul><li>When naming variables, keep the following rules in mind: </li></ul> <ul><li><ul><li>Variable names must begin with an alphabetic character. </li></ul></li></ul> <ul><li><ul><li>Following the first character, letters, numbers, and underscores are allowed. </li></ul></li></ul> <ul><li><ul><li>Variable names cannot include spaces. </li></ul></li></ul> <ul><li><ul><li>Variable names can be 255 characters long. </li></ul></li></ul> <p> 10. Rules for Naming Variables (cont.) 11. Using Variables </p> <ul><li>Variables can be used in the same way as labels and text boxes. </li></ul> <ul><li>Use the assignment operator to assign a value to a variable. </li></ul> <ul><li><ul><li>You can also assign hard-coded values to a variable. </li></ul></li></ul> <p> 12. Using Variables (cont.) </p> <ul><li>Use mathematical operators to perform calculations with numeric variables. </li></ul> <ul><li>Output the value in a variable. </li></ul> <ul><li><ul><li>Assign the value to a label. </li></ul></li></ul> <p> 13. Using Variables (cont.) 14. Using Variables (cont.) 15. Scope </p> <ul><li>The term scope refers to the reach of a variable. </li></ul> <ul><li>General rule </li></ul> <ul><li><ul><li>You should declare variables as locally as possible. </li></ul></li></ul> <p> 16. Three Levels of Scope </p> <ul><li>Levels </li></ul> <ul><li><ul><li>Local variable </li></ul></li></ul> <ul><li><ul><li><ul><li>Declared within an event procedure </li></ul></li></ul></li></ul> <ul><li><ul><li>Form-level variable </li></ul></li></ul> <ul><li><ul><li><ul><li>Declared in the Declarations section of a forms Code window </li></ul></li></ul></li></ul> <ul><li><ul><li>Global variable </li></ul></li></ul> <ul><li><ul><li><ul><li>Declared in a code modules section </li></ul></li></ul></li></ul> <p> 17. The Declarations Section </p> <ul><li>Access the Declarations section </li></ul> <ul><li><ul><li>Select (Declarations) from the Method Name list that appears at the top of the Code window. </li></ul></li></ul> <p> 18. The Declarations Section (cont.) 19. The Declarations Section (cont.) 20. Using the Object Data Type </p> <ul><li>Object data type </li></ul> <ul><li><ul><li>Very flexible </li></ul></li></ul> <ul><li><ul><li>Can store many different types of variables </li></ul></li></ul> <ul><li>Object variables </li></ul> <ul><li><ul><li>Can be declared by specifying the Object type </li></ul></li></ul> <ul><li><ul><li>Or by declaring a variable without a type </li></ul></li></ul> <p> 21. Summary </p> <ul><li>Data can be in the form of numbers, text, dates, pictures, and even sound. </li></ul> <ul><li>Visual Basic supports a set of data types. There are data types for whole numbers, floating-point numbers (decimals), text, dates, and more. </li></ul> <ul><li>You can choose to store data in memory locations called variables. </li></ul> <p> 22. Summary (cont.) </p> <ul><li>The AutoSize property will adjust the size of a control to fit its contents. </li></ul> <ul><li>The first step to using a variable is to declare it using theDimstatement. </li></ul> <ul><li>When naming variables, keep the naming rules in mind. It is a good idea to use naming prefixes to identify the data type of the variable.</li></ul> <p> 23. Summary (cont.) </p> <ul><li>You can assign values to variables using the assignment operator. </li></ul> <ul><li><ul><li>You can also use the other mathematical operators with numeric variables. </li></ul></li></ul> <ul><li>A variables scope indicates the procedures that have access to the variable. A variables scope can be local, form-level, or global. </li></ul> <p> 24. Summary (cont.) </p> <ul><li>The Declarations section of a forms Code window allows you to declare form-level variables. </li></ul> <ul><li>The Object data type can hold many different kinds of data, but is less efficient than specific data types. </li></ul>