Items in Visual Basic programs

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
JavaScript, Third Edition
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Basic Elements of C++ Chapter 2.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Objectives You should be able to describe: Data Types
Variables and Constants
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Programming with RAPTOR
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Computers, Variables and Types Engineering 1D04, Teaching Session 2.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
CPS120: Introduction to Computer Science Variables and Constants.
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 3: Using Variables.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Bill Tucker Austin Community College COSC 1315
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
The Selection Structure
Variables, Expressions, and IO
Basic Elements of C++ Chapter 2.
Variables and Arithmetic Operations
Microsoft Visual Basic 2005 BASICS
C++ Data Types Data Type
Primitive Types and Expressions
Unit 3: Variables in Java
Variables and Constants
Presentation transcript:

Items in Visual Basic programs Program statement: A program statement is a line of code in a Visual Basic program, a self-contained instruction executed by the Visual Basic compiler that performs useful work within the application. All program statements must follow syntax rules defined and enforced by the Visual Basic compiler. In Visual Studio 2005, program statements can be composed of keywords, properties, object names, variables, numbers, special symbols, and other values.

Items in Visual Basic programs Keyword: A keyword is a reserved word within the Visual Basic language that is recognized by the Visual Basic compiler and performs useful work. Keywords are one of the basic building blocks of program statements; they work together with objects, properties, variables, and other values to form complete lines of code and (therefore) instructions for the compiler and operating system.

Items in Visual Basic programs Control: A control is a tool you use to create objects on a Visual Basic form. You select controls from the Toolbox and use them to draw objects with the mouse on a form. You use most controls to create user interface elements, such as buttons, picture boxes, and list boxes.

Items in Visual Basic programs Object: An object is a user interface element that you create on a visual Basic form with a control in the Toolbox. In Visual Basic, the form itself is also an object. You can move, resize, and customize objects by using property settings. Objects have what is known as inherent functionality, they know how to operate and can respond to certain situations on their own. You can program Visual Basic objects by using customized event procedures for different situations in a program.

Items in Visual Basic programs Class: A class is a blueprint or template for one or more objects that defines what the object does. Accordingly, a class defines what an object can do but is not the object itself. In Visual Basic 2005, you can use existing Visual Studio classes (like System,Math and System.Windows.Forms.Form), and you can build your own classes and inherit properties, methods, and events from them. (Inheritance allows one class to acquire the pre-existing interface and behaviour characteristics of another class.)

Items in Visual Basic programs Property: A property is a value, or characteristic, held by an object. For example, a button object has a Text property to specify the text that appears on the button and an Image property to specify the path to an image file that should appear on the button face. In Visual Basic, properties can be set at design time by using the Properties window or at run time by using statements in the program code.

Items in Visual Basic programs Event procedure : An event procedure is a block of code that's executed when an object is manipulated in a program. For example, when the Button1 object is clicked, the Button1_click event procedure is executed. Event procedures typically evaluate and set properties and use other program statements to perform the work of the program.

Items in Visual Basic programs Method : A method is a special statement that performs an action or a service for a particular object in a program. In program code, the notation for using a method is object.Method( value) where Object is the name of the object you want to work with, Method is the action you want to perform, and Value is an optional argument to be used by the method.

Items in Visual Basic programs Methods and properties are often identified by their position in a collection or object library, so don't be surprised if you see long references such as System.Drawing.Image.FromFile, which would be read as "the FromFile method, which is a member of the Image class, which is a member of the System. Drawing object library."

Items in Visual Basic programs Variable : A variable is a temporary storage location for data in your program. You can use one or many variables in your code, and they can contain words, numbers, dates, properties, or other values. By using variables, you can assign a short and easy to remember name to each piece of data you plan to work with.

Variables can hold information entered by the user at run time, the result of a specific calculation, or a piece of data you want to display on your form. In short, variables are handy containers that you can use to store and track almost any type of information. Using variables in a Visual Basic program requires some planning. Before you can use a variable, you must set aside memory in the computer for the variable's use.

Variable Declaration To declare a variable in Visual Basic 2005, type the variable name after the Dim statement. (Dim stands for dimension.) This declaration reserves room in memory for the variable when the program runs and lets Visual Basic know what type of data it should expect to see later. Although this declaration can be done at any place in the program code (as long as the declaration happens before the variable is used), most programmers declare variables in one place at the top of their event procedures or code modules. For example, the following statement creates space for a variable named LastName that will hold a textual, or string, value: Dim LastName as String

Variable Declaration Dim LastName as String Note that in addition to identifying the variable by name, we've used the As keyword to give the variable a particular type, and identified the type by using the keyword String. A string variable contains textual information: words, letters, symbols-even numbers. Why do you need to declare variables? Visual Basic wants you to identify the name and the type of your variables in advance so that the compiler can set aside the memory the program will need to store and process the information held in the variables. Finally, keep in mind another important characteristic of the variables is their scope.

Variable Naming Conventions You need to use names that are short but intuitive and easy to remember. Begin each variable name with a letter or underscore. This is a Visual Basic requirement. Variable names can contain only letters, underscores, and numbers. Although variable names can be virtually any length, try to keep them under 33 characters to make them easier to read. Make your variable names descriptive by combining one or more words when it makes sense to do so. Use a combination of uppercase and lowercase characters and numbers.

Variable Naming Conventions An accepted convention is to capitalize the first letter of each word in a variable; for example, DateOfBirth. However, some programmers prefer to use so-called camel casing (making the first letter of a variable name lowercase) to distinguish variable names from functions and module names, which usually begin with uppercase let­ters. Don't use Visual Basic keywords, objects, or properties as variable names. If you do, you'll get an error when you try to run your program. Optionally, you can begin each variable name with a two-character or three character abbreviation corresponding to the type of data that's stored in the vari­able. For example, use strName to show that the Name variable contains string data. (sometimes called the Hungarian Naming Convention)

Using Variables One practical use for a variable is to hold information from the user. Although you can often use an object such as a list box or a text box to gather this information, at times you might want to deal directly with the user and save the input in a variable rather than in a property. One way to gather input is to use the InputBox function to display a dialog box on the screen and then use a variable to store the text the user types. FullName = InputBox(prompt) You can display the contents of a variable by assigning the variable to a property (such as the Text property of a label object) or by passing the variable as an argument to a dialog box function. One useful dialog box function for displaying output is the MsgBox function. When you call the MsgBox function, it displays a dialog box, sometimes called a message box, with various options that you can specify. Like InputBox, it takes one or more arguments as input, and the results of the function call can be assigned to a variable. MsgBox(FullName, , “Result”)

Variable Types Select data type… To handle… CTS type Example Boolean True or false conditions Value True Select data type… To handle… CTS type Example Short, Integer, Long, Byte Whole integers 23 (Integer) Single, Double, Decimal Numbers with integers and fractional parts 9456.72 (Decimal) Date Date and time values 02/12/2003 12:30:42 A.M. String Printable and displayable characters Reference “House” Object A pointer to the value of an object myClass myPerson

Summary of Variable Types Visual Basic .NET type Storage Size Range of Values Boolean 2 bytes True or False Date 8 bytes 0:00:00 on January 1, 0001 to 11:59:59 P.M. on December 31, 9999 Decimal 16 bytes Up to 29 significant digits with values up to 7.9228 x 10 (signed) 28 Double 8 bytes -4.94065645841246544E-324 to +1.79769313486231570E+308 (signed) Integer 4 bytes -2,147,483,648 to +2,147,483,647 (signed) Single 4 bytes -3.4028235E+38 to 1.401298E-45 (signed) String Varies 0 to approximately 2 billion Unicode characters (unsigned)

User-Defined Data Types Visual Basic also lets you create your own data types. This feature is most useful when you're dealing with a group of data items that naturally fit together but fall into different data categories. You create a user-defined type (UDT) by using the Structure statement, and you declare variables associated with the new type by using the Dim statement. Be aware that the Structure statement cannot be located in an event procedure-it must be located at the top of the form along with other variable declarations, or in a code module. For example, the following declaration creates a user-defined data type named Employee that can store the name, date of birth, and hire date associated with a worker: structure Employee Dim Name AS String Dim DateofBirth As Date Dim HireDate As Date End structure

Constants If a variable in your program contains a value that never changes, you might consider storing the value as a constant instead of as a variable. A constant is a meaningful name that takes the place of a number or a text string that doesn't change. Constants are useful because they increase the readability of program code, they can reduce programming mistakes, and they make global changes easier to accomplish later. Constants operate a lot like variables, but you can't modify their values at run time. They are declared with the Const keyword, as shown in the following example: const pi As Double = 3.14159265

Scope The location you choose for your declarations should be based on how you plan to use the constants or the variables. Programmers typically keep the scope for declarations as small as possible, while still making them available for code that needs to use them. For example, if a constant is needed only in a single event procedure, you should put the constant declaration within that event procedure. However, you could also place the declaration at the top of the form's code, which would give all the event procedures in your form access to it

VB Operators A formula is a statement that combines numbers, variables, operators, and keywords to create a new value. Visual Basic contains several language elements designed for use in formulas. Mathematical operators are the symbols used to tie together the parts of a formula. Operators + - * / \ Mod ^ &

Shortcut Operators Shortcut operators for mathematical and string operations that involve changing the value of an existing variable. For example, if you combine the + symbol with the = symbol, you can add to a variable without repeating the variable name twice in the formula. Thus, you can write the formula X = X + 6 by using the syntax X += 6.

Shortcut Operators Addition(+) X=X+6 X+=6 Subtraction (-) X =X-6 X -= 6 Multiplication (*) X = X*6 X *= 6 Division (I) X = X / 6 X /= 6 Integer division (\) X = X \ 6 X \= 6 Exponentiation (^) X = X^ 6 X ^= 6 String concatenation (&) X = X & 'ABC X &= 'ABC'

Precedence Visual Basic lets you mix as many mathematical operators as you like in a formula, as long as each numeric variable and expression is separated from another by one operator. Total = 10 + 15 * 2 / 4 ^ 2 The formula processes several values and assigns the result to a variable named Total. But how is such an expression evaluated by Visual Basic? In other words, what sequence does Visual Basic follow when solving the formula? You might not have noticed, but the order of evaluation matters a great deal in this example.

Precedence Values within parentheses are always evaluated first Exponentiation (raising a number to a power) is second. Negation (creating a negative number) is third. Multiplication and division are fourth. Integer division is fifth. Remainder division is sixth. Addition and subtraction are last.

Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression is a part of a complete program statement that asks a True­or-False question about a property, a variable, or another piece of data in the program code. For example, the conditional expression price < 100 evaluates to True if the Price variable contains a value that is less than 100, and it evaluates to False if Price contains a value that is greater than or equal to 100.

Comparison Operators used in Conditional Expressions = Equal to <> Not equal to > Greater than < less than >= Greater than or equal to <= Less than or equal to

Boolean expressions Expressions that can be evaluated as True or False are also known as Boolean expressions, and the True or False result can be assigned to a Boolean variable or property. You can assign Boolean values to certain object properties or Boolean variables that have been created by using the Dim statement and the As Boolean keywords.