Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

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.
XP New Perspectives on Microsoft Access 2002 Tutorial 61 Microsoft Access 2002 Tutorial 6 – Creating Custom Reports.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Slide 1 Chapter 2 Visual Basic Interface. Slide 2 Chapter 2 Windows GUI  A GUI is a graphical user interface.  The interface is what appears on the.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Visual Basic Chapter 1 Mr. Wangler.
Variables and Constants
Chapter 3: Using Variables and Constants
CS0004: Introduction to Programming Variables – Numbers.
05/09/ Introducing Visual Basic Sequence Programming.
Introduction to Python
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Chapter 3 Introducing Visual Basic.NET. 3.1 Visual Basic.NET Windows Programming -Used to create Windows, Web, and Console applications -Uses predefined.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Chapter 3 Part 21 Displaying Output Chapter 3 Section 3.5Output Formatting Chapter 3 Section 3.6Group Boxes, etc. Chapter 3 Section 3.7Sample Program Chapter.
Excel Spreadsheet basics. Excel Sheets and Books  Spreadsheet: tool to analyze, chart and manage data for personal, business and financial use Worksheet:
VB.NET PROGRAMMING FINAL EXAM TEST REVIEW.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Basic Controls & Properties Chapter 2. Overview u VB-IDE u Basic Controls  Command Button  Label  Text Box  Picture Box u Program Editor  Setting.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
 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.
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.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
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.
Game Maker – Getting Started What is Game Maker?.
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 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)
CECS 5020 Computers in Education Visual Basic Variables and Constants.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Programming with Visual Basic.NET. Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons.
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.
Instructors: Sidra Ehsan Budoor Bawazeer CpCS 206.
© 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.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Bill Tucker Austin Community College COSC 1315
Visual Basic.NET Windows Programming
A variable is a name for a value stored in memory.
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
Variables and Arithmetic Operations
Variables, Expressions, and IO
Chapter 2 Visual Basic Interface
Variables and Arithmetic Operations
Microsoft Visual Basic 2005 BASICS
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
VB.NET PROGRAMMING FINAL EXAM TEST REVIEW.
CHAPTER FOUR VARIABLES AND CONSTANTS
Presentation transcript:

Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

2

3 GroupBox Control The GroupBox Control is used as a container for other controls. This is an example of a container control … a control that serves as a holding area for other controls, indicating that the controls are somehow related. More often than not you’ll find radio buttons inside of the GroupBox Control.

4 Radio Button Controls The Radio Button Control presents a set of choices … such as the number of years in the car loan. –Radio Buttons are placed in groups so that only ONE may be selected at a time. –In order to do this it is important that you draw the radio buttons directly into the GroupBox Control. –If you do it right, you won’t have to write any additional code to enforce this rule – it will just work! –If you want more than one group of radio buttons just add more than one GroupBox control! The prefix for naming radio button controls is “rad”. Example: radYears

5 GroupBox Control Properties A GroupBox Control can only have a rectangular shape. A GroupBox Control can have a label, indicated by its Text Property Value. When RadioButtons are added inside a GroupBox, only one can be selected at a time. In general, you don’t have to rename your GroupBoxes since you don’t usually write code for them. FYI though: the naming prefix is “grp”

6 Radio Button Properties As you’ve learned, only one radio button within a group can be on at once. The Checked Property keeps track of which one is selected. –The checked property is either True or False. –When one button in a group becomes true, all the others automatically become false. FlatStyle Property determines the 3D appearance of the control Text Property – defines the visible text that displays on the control.

7 Using the Layout Toolbar The Layout Toolbar contains tools that allow you to adjust the alignment, spacing, and size of any group of controls on the form. –This really simplifies the task of changing the size and position of the controls on your form! To select more than one control: Press and hold down the CTRL key and then click on the other controls! Most popular features on the layout toolbar: –Make same size –Align Lefts, Rights, Tops and Bottoms –Center Horizontally and Center Vertically –Increase or Decrease Spacing Between Controls

8 For Example: Selecting Multiple Controls and Using the Align Rights Button

9 Cool Shortcut to put in for the End-User It is helpful to the end user if they can just hit the enter key and have the program execute it’s intended purpose. That is done by setting the default button. Just select the AcceptButton property in the Form’s Property List. Just choose the button that should be the default!

10 Locking Controls on a Form Locking Controls – This disallows the ability to move controls or modify control sizes on a form during design time – protects you from yourself! Click on your form, then go up to Format on the menu bar, and select Lock Controls

11 Constants and Variables A value is a number or string that programmers use in the code. –For example 150, 0.83, 8.14, “yes” or “no” A variable represents a location in computer memory that can change values as the code executes A constant represents a location in computer memory that cannot be changed during execution

12 VARIABLES Variables are used in code statements to temporarily store values so that they may be used again in other calculations. Variables are created by programmers. They must have a name and a type. The name and type are decided on by the programmer. To decide upon the type you must determine what kind of data the variable will store..

13 Data Types The data type of a variable or constant determines what kind of data it can store. Here is a table with some basic.NET data types!

14 VARIABLE NAMES There are rules to naming your variables: –The name must begin with a letter, not a # ! –The name cannot be more than 16,383 characters. –The name cannot contain punctuation or blank spaces. The easiest way to create a variable is to assign a value to the variable’s name in a code statement. For ex: dblrate=3.5 or strName=“John”.

15 Naming Conventions for Variables You don’t have to remember all of these at once … you’ll get the hang of them as you start creating and naming variables! Just start the variable name off with the proper prefix. Then give it a name that describes what it is storing! –Short == shr –Integer == int –Long == lng –Byte == byt –Decimal == dec –Single == sng –Double == dbl –Char== chr –String == str –Boolean == bln –Date == dtm –Object ==obj

16 MORE ON VARIABLES Variables created in this way may hold either numbers (numeric data) or characters (string data) Character data is called a string and must be placed in quotation marks. Ex: name=“Christy” Examples of code statements that assign variables: –Numeric Data -- dblPrice = 5 –String Data -- strShow = “Hamlet” –Value of another variable --dblCost = dblprice –value of an expression -- dblCost = dblprice * dblsale –value of an expression -- dblprice = dblprice * 1.05 Note : the receiving variable -- must be on the left side.

17 Understanding the Option Strict Statement When you use assignment statements.NET does not require you to use the same data type on both sides of the equal sign..NET will attempt to make it work but this can lead to a bunch of errors at run time. –For example… lets say you declare a variable as a short and then try to store a value of 50,000 in it. Shorts only go up to about 32,000. This will cause the program to come to a screeching halt. The Option Strict Statement can be used to instruct Visual Basic.NET to force you to ensure that all assignment statements use the same data type on both sides of the equal sign.

18 Using Option Strict This makes it a bit more tedious to code BUT the benefit is that.NET will let you know when you are doing it wrong and prompt you to fix it! Being in Option Strict mode also means that you have to declare every variable! That’s good for beginning programmers. It keeps you honest! In the code window on line 1, type Option Strict On

19 DECLARING A VARIABLE Generally speaking, variables need to be declared. The computer needs you to tell it the name of your variable and what type of variable you want it to be. There are many different types of variables. –Some types are integer, string, single and double. To declare a variable you use the keyword DIM. –Example: Dim intNumber as integer Scope refers to a variables ability to be accessed throughout a program. It is defined by where the variable is declared. The scope can be limited to one procedure … for example: the variable will work within a given calculate button procedure OR it can be available throughout an entire form. Let’s give some examples:

20 Declaring Global Variables Global variables are those that are available to all the procedures on a given form. To declare a global variable type the Dim line in the Public Class Section of the Code Window. (Right below the REM Lines where you put your name!) Also, put a “g” in front of the name to indicate that it’s a global variable! For Example:

21 Declaring Local Variables Local variables are those that only work in a single procedure. To declare a local variable go to the specific event procedure where you want the variable to work and write the DIM statements there! For example: btnCalculate_Click Event

22 Declaring Constants Using the Keyword Const

23 Converting Data Types Because Option Strict is on in this chapter, you have to insure that all the data being used by the calculations has the same data type … if you don’t, you’ll get an error! For example: In the main project the nudLoanAmount box has a Decimal Value Type. Your calculation needs to go into a Double Variable. Therefore you need to convert it. Luckily,.NET has simple-to-use conversion statements. For example:

24 Coding a Form Load Event Procedure Every event has a default event … for command buttons it is the Click Event. For Forms it is the Form Load Event. This event executes when the form first loads into memory. Therefore, any code you have in the Form_Load Event will trigger whenever the form loads!

25 Numeric Expressions An expression can perform a calculation, manipulate characters, call a function, or test data. A numeric expression is any expression that can be evaluated as a number. –A numeric expression can include values, variables, constants, as well as certain control properties. Expressions often contain math problems … therefore arithmetic operators and order of operations come into play!

26 ARITHMETIC EXPRESSIONS VB follows the normal order of operations – which is a predetermined order of performing calculations within an expression. (PEMDAS) Arithmetic operators in Visual Basic –^ -- used to raise the power of an exponent –* -- used to multiply two numbers –/ -- used to divide 2 #’s and return a decimal result –\ -- used to divide 2 #’s and return an integer result –MOD -- Used to divide 2 #’s and return only the remainder –+ -- used to sum 2 #’s –- -- Used to subtract 2 #’s or indicate a negative value

27 Intrinsic Functions Sometimes you DON’T have to reinvent the wheel! Let’s say you want to do a calculation that millions before you have also wanted to do…. Perhaps it’s already set up for you? VB programmers created a set of Intrinsic Functions – functions that are built into.NET. You used one of those Intrinsic Functions in your main project. It was the PMT Function. –This is a financial function that will return the payment for a loan based on periodic, constant payments and a constant interest rate. –Pmt(rate, number of periods, pv, fv, due)

28 The Pmt Function

29 Using the Pmt Function This is what it looked like in your code! There is a list of additional intrinsic functions on page 4.59 in your book!

30 Using the Format$ Function The Format$ Function is an Intrinsic function that allows you to apply a “better look” to your output. For instance, what if you want to round an answer to a certain number of decimal places OR you want the answer to display as a $$$ value. Use the format$ function! Format$ -- function that allows the programmer to determine how the output will appear on the screen. –Ex: Format$( dblNum * dblprice, “currency”) –Some popular formats are general number, currency, fixed, standard and percent

31 The Format$ Function

Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 THE END!