250 likes | 360 Views
Data. Tonga Institute of Higher Education. Variables. Programs need to remember values. Ex: If you use a program to record sales, you will want to remember data: A loaf of bread was sold to Sione Latu on 14/02/04 for T$1.00. Customer Name: Sione Latu Date Sold: 14/02/04
E N D
Data Tonga Institute of Higher Education
Variables • Programs need to remember values. • Ex: If you use a program to record sales, you will want to remember data: • A loaf of bread was sold to Sione Latu on 14/02/04 for T$1.00. • Customer Name: Sione Latu • Date Sold: 14/02/04 • Item Sold: Loaf of Bread • Item Cost: T$1.00 • Variables – Places in the computer’s memory where we can store information. • Values and Objects are stored in variables. • Numbers • Characters • Strings • Dates
Numerical Data Types • Primitives - The basic types supported by the programming language.
Primitive Data Type Selection Practice • How to choose the right primitive variable type. • Byte, Short, Integer and Long store whole numbers. • Single, Double and Decimal store fractions. • If you pick something that is too big, you’re wasting memory space. Wasting memory space slows things down. • If you pick something that is too small, then you’re going to crash. • If you need a character, use Char. • If you need a true/false value, use Boolean. • What is a good data type for: • Someone’s age? • A customer’s identification number for a video rental store in Nuku’alofa? • A very large number with decimals? • The price of an item?
Using Variables • 2 Steps to using variables • Declare the variable • Initialize the variable
Declaring Variables – 1 • Declare the variable – Tell the computer to reserve a space in memory for the variable. • You need to tell the computer 2 things: • Name of the variable • Type of the variable (What kind of variable you have) • Types • Byte • Short • Integer • Long • Single • Double • Decimal • Boolean • Date • Char • String Type Name
Declaring Variables – 2 • Use a name that is easy to remember. • Do not use x, y, z • Begin each separate word in a name with a capital letter • Examples • FirstName • CustomerID
Initializing Variables • Initialize the variable – Assign an initial value to a variable. • Char values must be enclosed in double quotes and have a lowercase c next to it. • String values must be enclosed in double quotes. • Boolean value should be True or False Initial Value
Declaring and Initializing Variables in 1 line • You can declare and initialize a variable in 1 line.
Demonstration Declaring and Initializing Variables
Converting Variable Types • You can convert variable types through the Cast functions • CBool() • CDbl() • CObj() • CByte() • CDec() • CShort() • CChar() • CInt() • CSng() • CDate() • CLng() • CStr() • If the Cast function isn’t able to convert the value, you will get an error.
Demonstration Cast Functions
Converting Variable ValuesSmaller to Larger • It is possible to automatically move a value from a variable with a smaller type to a variable with a larger type • Example Maximum Short Value Maximum Long Value Is 9223372036854775807
Converting Variable ValuesLarger to Smaller • It is not possible to automatically move a value from a variable with a larger type to a variable with a smaller type • Using this: • Results in: • Try to not do this. If you must do this, then use the cast functions. Bigger than maximum Short value
Demonstration Converting Variable Values
Option Strict, Option Explicit, Option Compare - 1 • Option Strict • Use On to enforce the following rules: • Conversions must done by the developer • Option Explicit • Use On to enforce the following rules: • All variable names must be declared. • Option Compare • Use as needed. Binary is good for most cases. • Determines whether strings are compared as binary strings or text. • Binary – “A” is not equal to “a” • Text - “A” is equal to “a”
Option Strict, Option Explicit, Option Compare - 2 • Each project can use default settings. Set the default settings in Tools -> Options -> Projects -> VBDefaults • Each project can use it’s own settings. Set the project settings in Solution Explorer -> Project -> Right Click -> Properties -> Common Properties -> Build • Each file can use it’s own settings. Set the file settings using this code at the beginning of the code • Option Explicit On • Option Strict On • Option Compare Binary
Demonstration Option Strict, Option Explicit, Option Compare
Arithmetic Operators • Declare and Initialize x, y and z • Get values from x and y • Adds x and y together • Assigns the sum of x and y to z
Arithmetic Operator Details • Exponentiation • Use carrot (^) • Multiplication • User asterisk (*) instead of x • Division • Make sure that the variable that holds a fraction allows decimal points • Addition • Subtraction
Order of Operations • When you have a lot of operations, they are performed in a certain order. • Operations in Parentheses () • Exponentiation operations from left to right • Multiplication or Division operations from left to right • Addition or Subtraction operations from left to right • Please Excuse My Dear Aunt Sally. • Examples: • 3 + 6 + 9 / 3 = 3 + 6 + 3 = 12 • (3 + 6 + 9) / 3 = 18 / 3 = 6 • (5 + 3) / 2 ^ 2 = 8 / 2 ^ 2 = 8 / 4 = 2
String Concatenation • String Concatenation – Adding strings together • Add strings using + or & • Most developers use & because it is better at automatically converting values
String Concatenation - 2 • There are 2 ways to add carriage returns to a string • Vbcrlf • This is how this was done in previous versions of VB • Environment.NewLine • This is the new way • Sometimes, string concatenation results in very long lines of code. You can use _ to break up 1 long line into multiple smaller lines. • Remember to follow code conventions • Indent all following lines • Put the & or + at the beginning of the next line
Demonstration String Concatenation