1 / 25

Data

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

murray
Download Presentation

Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Tonga Institute of Higher Education

  2. 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

  3. Numerical Data Types • Primitives - The basic types supported by the programming language.

  4. Non-Numerical Data Types

  5. 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?

  6. Using Variables • 2 Steps to using variables • Declare the variable • Initialize the variable

  7. 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

  8. 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

  9. 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

  10. Declaring and Initializing Variables in 1 line • You can declare and initialize a variable in 1 line.

  11. Demonstration Declaring and Initializing Variables

  12. 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.

  13. Demonstration Cast Functions

  14. 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

  15. 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

  16. Demonstration Converting Variable Values

  17. 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”

  18. 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

  19. Demonstration Option Strict, Option Explicit, Option Compare

  20. 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

  21. 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

  22. 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

  23. String Concatenation • String Concatenation – Adding strings together • Add strings using + or & • Most developers use & because it is better at automatically converting values

  24. 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

  25. Demonstration String Concatenation

More Related