Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 3: Using Variables.

Similar presentations


Presentation on theme: "Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 3: Using Variables."— Presentation transcript:

1 Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 3: Using Variables

2 Objectives  Differentiate between Variables, Input, and Output  Describe the various data types that you can use to declare variables.  Name, declare, initialize, and use variables and constants.  Declare variables with different levels of scope.  Create user-defined data structures.  Convert variable values from one data type to another.  Store data in arrays.

3 3 Variables  Variables store values that can change when an application is running  Variables have six basic elements: ElementDescription NameThe word that identifies the variable in code AddressThe memory location at which the value is stored Data typeThe type and starting size of data the variable can store ValueThe value at the variable’s address ScopeThe set of all code that can access and use the variable LifetimeThe period of time for which a variable is valid

4 4 Definition  You often need to store values temporarily when performing calculations in Visual Basic. For example, you might want to calculate several values, compare them, and perform different operations on them depending on the result of the comparison.  You use variables to store values that can change when an application is running.

5 Variable elements  A variable has the following six elements: 5 ElementDescription NameThe word that identifies the variable in code AddressThe memory location at which the value is stored Data typeThe type and starting size of data the variable can store ValueThe value at the variable’s address ScopeThe set of all code that can access and use the variable LifetimeThe period of time for which a variable is valid

6 Examples of variables  A variable can be used in many ways, including the following: As a counter that stores the number of times As temporary storage for property values As a location to hold a value returned from a function As a location to store directory or file names 6

7 How to Name Variables  Naming rules Start with an alphabetic character or underscore Do not use spaces or symbols Do not use keywords such as Integer  Examples of variable names CustomerName (PascalCasing) accountBalance(camelCasing)

8 How to Name Variables Rules  When you declare a variable, it is important to develop a naming strategy. Both clarity and consistency are important, especially when others will need to read or maintain your code.  When you name a variable in Visual Basic.NET, you must observe the following rules: Start each variable name with an alphabetic character or an underscore (_). Do not use spaces or symbols. Integer or Date Do not use keywords such as Integer or Date.

9 How to Name Variables Recommendations  It is recommended that you observe the following guidelines when naming variables: Provide meaningful, descriptive names, such as accountNumber. Although typing a long variable name can be tedious when writing code, it will make your code more readable and easier to maintain. Begin each separate word in the name for a public variable with a capital letter, as in CustomerName. This is called PascalCasing. Avoid using abbreviations.

10 How to Name Variables Recommendations  Although you can use a variable name such as acctno, it is much easier top read your code if you use the name accountNumber. If you must use abbreviations, make sure they are consistent throughout the entire application.  Use a unique name within the scope of the variable. Scope refers to the set of all code that recognizes the variable.  When declaring local and private variables, start the first word with a lowercase character, as in newCustomer. This is called camelCasing.

11 How to Declare Variables  Syntax to declare variables Dim variableName As Type  Examples of value-type variables Dim numberBooks As Integer Dim squareFootage As Single  Examples of reference-type variables Dim myForm As Form Dim userInput As String

12 Syntax Dim  You declare a variable to specify its name and characteristics. The declaration statement for both value-type and reference-type variables is the Dim statement.  The declaration’s location and contents determine the variable’s characteristics.  To declare a variable, use the following syntax: Dim variableName As Type Dim AsDim  You use the Dim statement to declare and allocate storage space for variables in blocks, procedures, modules, structures, and classes. You use the As clause in the Dim statement to specify the variable’s data type. NoteDim Note The keyword Dim is an abbreviation of the word dimension.

13 How to Assign Values to Variables  You can : Assign a value to a variable after you declare it Dim birthday As Date Birthday = #3/9/1974# Assign a value to a variable when you declare it Dim birthday As Date = #3/9/1974#

14 Syntax  Before you can use variables in your application, you need to assign values to them. You can assign a value to a variable after you declare it or when you declare it.  To assign a value to a variable, use the assignment operator (=), as shown in the following expression:VariableName=Value  The value on the right side of the expression is assigned to the variable on the left side of the expression.  You can assign values to variables after you declare them, as shown in the following example: Dim birthday As Date Birthday = #3/9/1974#

15 Syntax  When you create a variable with the Dim statement, Visual Basic automatically initializes numeric variables to 0, text strings to empty (“”), and date variables to January 1, 0001.  Alternatively, you can assign a value to a variable when you declare it, as shown in the following examples: Dim birthday As Date = #3/9/1974# Dim goodNews As String = “ Your check is in the mail ” Dim testCondition As Boolean = True Note Date values must be enclosed in number symbols (##), and String values must be enclosed in quotation marks (“”).

16 Creating and Using Structures  What Are Structures? A structure is a composite data type that is created by combining other data types. Structures are value types.that is, a variable of a structure type contains the structure.s data, rather than containing a reference to the data as a reference type does. Structures can have data, properties, methods, and procedures, and can raise and handle events. 16

17 Creating and Using Structures (Example)  The simplest and most common use of structures is to encapsulate related variables, creating a user-defined data type. For example, you might want to keep an employee.s name, hire date, job title, and salary together. You could use several variables for this information, or you could define a structure and use it for a single employee variable. The advantage of the structure becomes apparent when you have many employees and therefore many instances of the variable. 17

18 Creating and Using Structures (Example)  The simplest and most common use of structures is to encapsulate related variables, creating a user-defined data type. For example, you might want to keep an employee.s name, hire date, job title, and salary together. You could use several variables for this information, or you could define a structure and use it for a single employee variable. The advantage of the structure becomes apparent when you have many employees and therefore many instances of the variable. 18

19 Creating and Using Structures (Example)  The following example shows a simple Employee structure: Public Structure Employee Public FirstName As String Public LastName As String Public HireDate As Date Public JobTitle As String Private Salary As Decimal End Structure 19

20 Storing Data in Arrays What Is an Array? 20

21 Storing Data in Arrays What Is an Array?  An array is a fundamental data type in Visual Basic, as it is in most programming languages. You can use arrays to create shorter and simpler code in many situations because you can set up decision structures and loops to retrieve and modify any element in an array. 21

22 Storing Data in Arrays What Is an Array?  An array is a sequence of data elements of the same type. You can access individual elements in an array by using the array name and an index or indexes (starting at 0) to specify the element.s position in the array. An array has one or more dimensions with one or more elements in each dimension. 22

23 Storing Data in Arrays What Is an Array? (Example)  To declare the array described in the preceding analogy, you can use the following code:  You can access the third house in the countHouses array as follows: 23


Download ppt "Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 3: Using Variables."

Similar presentations


Ads by Google