Making Interactive Programs with Visual Basic .NET

Slides:



Advertisements
Similar presentations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Advertisements

Types and Arithmetic Operators
1.
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.
CIS 115 Lecture 5.  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can be referred.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Visual Basic Chapter 1 Mr. Wangler.
Variables and Constants
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Review for Mid-term! October 26, Review Homework Worksheet True or False Operators are symbols that perform specific operations in Visual Basic.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Chapter 2: Using Data.
Representing Data: Constants and Variables CHAPTER THREE Matakuliah: T0063 – Pemrograman Visual Tahun: 2009.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Introduction to Programming with RAPTOR
Mathematical Calculations in Java Mrs. G. Chapman.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Applications Development
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
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.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
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.
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
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.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
CHAPTER THREE Representing Data: Constants and Variables.
Controlling Program Flow with Decision Structures.
Variables in VB. What is a variable? ► A named memory location that stores a value.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Programming with Visual Basic.NET. Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons.
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.
Visual Basic/ Visual Studio Brandon Large. Connecting to prior knowledge In your notes write down what the two main parts of the computer are. The “software”
Introduction to Computer CC111 Week 09 Visual Basic 2 Visual Basic Variables 1.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic.NET Windows Programming
A variable is a name for a value stored in memory.
Visual Basic Variables
Variables and Arithmetic Operations
Variables and Arithmetic Operations
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
Presentation transcript:

Making Interactive Programs with Visual Basic .NET

Quick Links Using Variables Built-in data types Option Explicit Syntax Errors The Toolbox Control

Using Variables A variable is a named memory location that stores a value Example: Imagine we are creating a program that takes in a user’s age as input and then displays the following messages depending on the age: If the user’s age < 18, display “Too young to vote” If the user’s age >= 18, display, “You can vote” If we are creating this program, we need to get the input and store it in a variable (memory)

Using Variables To get this input, we use a control object such as a TextBox control. Variables come into play when we want to store this information in memory for later use We do this in VB .NET by first declaring a variable. This is done with the following syntax: Dim age As Integer “Reserve a space in memory called ‘age’ and save enough space for an integer” Variable name

Variable Assignment Now that we have declared a variable, we can start using it to store Integer values using assignment age = 27 ‘Now age stores the value 27 age = txtMyTextBox.Text ‘Now age stores the value ‘that the user entered into ‘the textbox This is wrong, however: 27 = age Good programming style to give variables meaningful names and to declare them at the beginning of the procedure

Some Variable Examples A variable can only store one value at any given time An expression can also be used on the right side of an assignment statement. Remember that the expression on the right side of an assignment is evaluated first and then assigned to the variable on the left side of the = sign. Dim ageInYears As Integer Dim ageInDays As Integer ageInYears = 27 ageInDays = ageInYears * 365 ‘Not including leap years

Built-in Data Types A data type defines the format of data and how it is stored on a computer VB .NET has several built-in data types: Type Used to Represent Single Numbers possibly containing a decimal Double Integer Integers (no decimals) Long String A set of characters Boolean True or False

Variable Naming Conventions Variable identifiers should have descriptive names and begin with an appropriate prefix as listed below Type Prefix Single sgl Double dbl Integer int Long lng String str Boolean bln

Naming Convention Examples Dim strLastName As String ‘ User’s last name Dim intAge As Integer ‘ User’s age Dim dblHeight As Double ‘ User’s height in cm Dim blnVote As Boolean ‘ True if user is old ‘ enough to vote; ‘ false otherwise intAge = 17.4 ‘ ok, but doesn’t work as ‘ expected. intAge stores 17 dblHeight = 175.36 ‘ dblHeight stores 175.36

Variable Declarations A single Dim statement can be used to declare multiple variables of different types Dim strName As String, intAge As Integer VB .NET initializes variables when they are declared Numeric types (Double, Integer…) are initialized to 0 String variables are initialized to “” (empty string) Boolean variables are initialized to False

Option Explicit Dim intAge, intAgeInDays As Integer intAg = 20 ‘ Misspelled variable – we never ‘ declared intAg intAgeInDays = intAge * 365 ‘ Gives 0 since ‘intAge was never set and was initialized to 0 by ‘VB .NET The Option Explicit statement can be added to the general section of the form module. Option Explicit displays an error message at run time when a variable is used before it is declared.

Syntax Errors A statement that violates the rules of VB .NET is said to contain a syntax error Example: Dim intAge As Integer = 12 ‘Syntax error! This is a syntax error because assignment is illegal in a variable declaration Syntax errors are detected by VB .NET and the error is underlined in blue in the code window Other types of errors are not detected until the program is run, at which time, a run-time error is generated

The TextBox Control Text boxes are used to get textual input from the user. Examples: User Name, height, age… Textbox

The TextBox Control TextBoxes have the properties: Name: identifies the object and is used by the programmer. Good style to prefix with txt. Text: The text that is displayed in the text box. The user can change this text. Alignment: Sets the alignment of text relative to the text box.

A TextBox Control Example TextBox to get age in Years txtAgeInYears Calculates age in days btnCalc Label that will display the result to the user lblAgeInDays

A TextBox Control Example The following code goes in the event procedure for the button Dim intAgeInYears, intAgeInDays As Integer intAgeInYears = Me.txtAgeInYears.Text ‘ Get user input from ‘ text box intAgeInDays = intAgeInYears * 365 ‘ Calc age in days Me.lblAgeInDays.Text = intAgeInDays ‘ Display the result in ‘ the label

Integer Division In addition to (+, -, /, *, ^), VB .NET includes integer division (\) The \ operator truncates the decimal portion of the quotient Example Dim intX As Integer intX = 20 \ 7 ‘ intX is assigned 2

Modulus In addition to (+, -, /, *, ^, \), VB .NET includes the Modulus operator Modulus division returns the remainder resulting from division Example Dim intX As Integer intX = 20 Mod 7 ‘ intX is assigned 6 You can use long division to find the remainder of 20 divided by 7 which is 6

New Order of Operations Brackets ( () ) Exponents ( ^ ) Division ( / ) Multiplication ( * ) Integer Division ( \ ) Modulus Division ( Mod ) Addition ( + ) Subtraction ( - )

Automatic Type Conversion In assignment, VB .NET automatically converts data to match the type of the variable it is being assigned to Examples Dim intX As Integer intX = 6.7 ‘ intX is assigned 7 intAgeInYears = Me.txtAgeInYears.Text ‘ Assigns Text that user entered (which is ‘hopefully a number) to an Integer variable

Using Named Constants A constant is a named memory location which stores a value that cannot be changed at runtime from its initial assignment Example Const dblPi As Double = 3.14 dblPi = 29 ‘ This is not allowed ‘ dblPi cannot be changed ‘ at runtime