CECS 5020 Computers in Education Visual Basic Variables and Constants.

Slides:



Advertisements
Similar presentations
CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
Advertisements

PL/SQL.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Introduction to C Programming
1 Pertemuan 04 Expression Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Developing Software Applications Introduction to Variables & Data Types in VB State Transition Diagrams.
Introduction to a Programming Environment
Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic.
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Chapter 8: String Manipulation
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Variables and Constants
Chapter 4: The Selection Process in Visual Basic.
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Manipulating Strings. What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII.
Visual Basic Programming
Applications Development
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
INT213-Week-2 Working with Variables. What is variable
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Higher Computing Software Development -So Far- 5/10/10.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
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.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
VISUAL BASIC 6.0 Designed by Mrinal Kanti Nath.
Introduction to Programming Lecture 2
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Visual Basic 6 (VB6) Data Types, And Operators
3rd prep. – 2nd Term MOE Book Questions.
VB Math All of your favorite mathematical operators are available in VB: + Addition - Subtraction * Multiplication / Division \ Integer Division Mod Modulo.
Variables and Arithmetic Operations
Chapter 6 Variables What is VBScript?
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Chapter (3) - Looping Questions.
T. Jumana Abu Shmais – AOU - Riyadh
Selection Statements.
Fundamentals of visual basic
Presentation transcript:

CECS 5020 Computers in Education Visual Basic Variables and Constants

CECS 5020 Variables u Variables are containers or place holders u Variables store different types of data u Data = Numbers, Characters, Strings u Assign variables using the equal operator 0 = “Let the variable counter have the value of 5.” After the assignment, the previous value of counter is destroyed counter

CECS 5020 Declaring Variables u Declaring a variable tells VB the variable’s type u It’s good practice to insert the command “Option Explicit” in the General section of your code to remind you to declare all variables u Variables are declared with the “Dim” (dimension) command. Examples: Dim num1 as Integer Dim str1 as String

CECS 5020 Numeric Variables and Operators u Byte u Integer u Long u Single u Double u Currency u Dec u ^ Exponentiation u - Negation u * Multiplication u / Division u \ Integer division u + Addition u - Subtraction u Mod Modulo

CECS 5020 String Variables and Operators u Variable-length (up to 2 billion bytes) u Fixed-length (up to 64,000 bytes) u You usually use variable-length strings in your code u Operators: & Concatenation (“to gether together”)

CECS 5020 Other Variable Types u Boolean (True or False, Yes or No) u Boolean operators: –AND –OR –NOT u Date (Between Jan. 1, 100 and Dec. 31, 9999). Also stores hours and seconds.

CECS 5020 Useful Date Functions u Now() - Returns current date and time u Date - Returns current date u Time - Returns current time u IsDate(string) - Returns a Boolean saying whether string is a valid date u DateValue(date) - Returns a Date value from a string in date form u Weekday(date) - Returns the serial number of the day of the week of date

CECS 5020 The Immediate Window and Debug.Print u Debug.Print “prints” to the immediate window u Useful for checking (“debugging”) your code u Example: Debug.Print “The value of icount is “ & icount

CECS 5020 Useful String Functions u Len() - Returns the length of a string u InStr() - Searches for a substring within a string u Left(), Right(), Mid() - Return portions of a string from the left, right, or middle of the string

CECS 5020 Useful String Functions u Ltrim(), Rtrim(), Trim() - Remove spaces from the left, right, or both ends of strings u Chr() - Returns the ASCII character corresponding to a numeric value u StrComp() - Compares two strings

CECS 5020 Commenting Your Code u Use the ‘ (apostrophe) to start a comment u Anything after the ‘ is not executed u Comments promote readability, user understanding u Use them whenever something needs to be explained (not “assign the variable”, etc.)

CECS 5020 Immediate Execution u Start a new project and add “Option Explicit” and “Dim icount as integer” to the General section of the code u Add “count = 1001” to the Load event of the form u Add a button with the caption “Test” u Run the program

CECS 5020 Immediate Execution u Use the “pause” button on the VB tool bar to interrupt your code u In the Immediate window, enter “Print icount” and press enter u Enter “icount = icount + 2”, and print icount again

CECS 5020 Program Flow and the If statement u VB code is executed from top to bottom unless something re-directs the flow u The If statement makes decisions about program flow and execution u Syntax: If [expression] then Statement(s) [Else Statement(s)] End If

CECS 5020 Example of an If Statement If MyTxtBox.Text = “Yes” Then count = count + 3 MsgBox “The count was increased” Else count = count - 3 MsgBox “The count was decreased” End If

CECS 5020 Nesting If Statements u You can “nest” if statements: If count <= 0 Then If count > Then MsgBox “Count is between 0 and -1000” Else MsgBox “Count is less than -1000” End If Else MsgBox “Count is greater than 0” End If

CECS 5020 Comparison Operators u = Equality u <> Inequality u < Less Than u > Greater Than u <= Less Than or Equal u >= Greater Than or Equal

CECS 5020 Constants u Constants allow you to use mnemonic names for values that never change u They are declared like variables, except with the Const statement: Const count = 1001 Const sMyName = “John James Harrison” u Use constants to clarify your code u VB has a number of supplied constants: see them in the Object Browser

Sample Code u Add three TextBoxes to the form, naming them “First”, “Second”, and “Result” u Replace the button code with: ‘ Simple test of which of two strings is longer and puts result in the ‘ Result textbox If Len(First.Text) > Len(Second.Text) then If Left(First.Text,1) >= “A” and Left(First.Text,1) <= “Z” Then Result.Text = “First is longer: starts with Upper case” Else Result.Text = “First is longer: doesn’t start with upper case” End If Else Result.Text = “Second is longer” End If