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

Slides:



Advertisements
Similar presentations
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Introduction to C Programming
1.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
JavaScript, Fourth Edition
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Introduction to C Programming
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
Basic Elements of C++ Chapter 2.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Storing data Getting data out Data types Ruby as a foundation Program Variables Click icon to hear comments (the download may take a minute)
Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
IE 212: Computational Methods for Industrial Engineering
Chapter 4: The Selection Structure
Tutorial 11 Using and Writing Visual Basic for Applications Code
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
CHAPTER THREE Representing Data: Constants and Variables.
Lecture 8 Visual Basic (2).
Representing Data: Constants and Variables CHAPTER THREE Matakuliah: T0063 – Pemrograman Visual Tahun: 2009.
Fundamentals of GUI Programming. Objectives: At the end of the session, you should be able to: describe the guidelines that are used for creating user-friendly.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Items in Visual Basic programs
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 7 Where Can I Store This?
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 4: Understand and implement Decisions.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
CHAPTER THREE Representing Data: Constants and Variables.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Data Handling in Algorithms. Activity 1 Starter Task: Quickly complete the sheet 5mins!
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic select statement
The Selection Structure
Chapter 4: The Selection Structure
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Microsoft Visual Basic 2005 BASICS
Chapter 6 Variables What is VBScript?
Chapter 3 The DATA DIVISION.
Variables ICS2O.
Chapter 2: Introduction to C++.
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

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

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 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 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.

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

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

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)

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.

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.

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.

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

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.

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#

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#

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,  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 (“”).

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

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

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

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

Storing Data in Arrays What Is an Array? 20

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

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

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