- 3. The Visual Basic
3. The Visual Basic .NET Language Learning to Program Overview - - PowerPoint PPT Presentation
3. The Visual Basic .NET Language Learning to Program Overview - - PowerPoint PPT Presentation
3. The Visual Basic .NET Language Learning to Program Overview The Common Language Runtime Variables, data types, constants and literals Identifiers Expressions, operators Statements and blocks Structured variables and
Overview
The Common Language Runtime Variables, data types, constants and
literals
Identifiers Expressions, operators Statements and blocks Structured variables and enumerations
VB .NET and The CLR
.NET programs execute on a virtual machine
called the .NET CLR
CLR = Common Language Runtime This is so that a .NET program can be run on any
computer provided a version of CLR is implemented for it
Also allows programs to be written for an ‘ideal’
computer that was created by programmers for programmers
CLR supports many programming languages – not
just Visual Basic
VB Running on a .NET Platform
A Visual Basic program
The .NET Common Language Runtime environment Computer hardware and Operating System
CLR Features
CLR has a number of useful features for
programmers
Common Type System
A range of variable types that represent all of the common
types of computer data
Numbers, dates, times, characters, text, currency etc.
Since all .NET languages use it, all are directly
compatible with each other
Can use a .NET class in a C# program etc.
Data types are independent of the computer or
- perating system used
Variables in Programs
Programs need to store and use data
Values input by the user Intermediate results in calculations Data read from disk, CD-ROM, other programs or the Internet Information indicating the current state of a program
How far through a list of values it has got The name of a data file etc.
Variables are elements of computer memory made
accessible by a programming language
Storage for a data value is given a name, or Identifier The Identifier is used in program statements to access (read or
write) the contents of a variable
The values stored in variables can change over time
(that’s why they are called Variables)
Program statements can update a value as necessary
Example use of variables
Sub Main() Dim length, width, area As Single length = 12.5 width = 8.75 area = length * width Console.WriteLine("Area is {0}", area) End Sub 12.5 18.5 231.25 Length Width Area Variables can be used to
represent physical values
e.g. The Length, Width and Area of a
rectangular item
Until a value is assigned, a
variable will usually contain 0
Data Types
The CTS provides a wide range of types
Numbers
Integers, real (floating point) numbers Various precisions
Characters and Text Dates and Times True or False/Yes or No
This range allows almost any physical or
logical value to be stored and manipulated
Types in Detail
A high precision number stored in a direct decimal representation to improve the accuracy
- f calculations (29 digits
- f precision)
+/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is +/-0.0000000000000000000000000001
Decimal
A double-precision floating point number stored in an efficient binary representation (15 digits
- f precision)
- 1.79769313486231E+308 to
- 4.94065645841247E-324 for negative values;
4.94065645841247E-324 to 1.79769313486231E+308 for positive values
Double
A single-precision floating- point number stored in an efficient binary representation (7 digits
- f precision)
- 3.402823E+38 to -1.401298E-45 for negative values;
1.401298E-45 to 3.402823E+38 for positive values
Single
A 64 bit signed number with no fractional part
- 9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
Long
A 32 bit signed number with no fractional part
- 2,147,483,648 to 2,147,483,647
Integer
Description Range Type Name
Types in Detail II
Description Range Type Name Dates, times and combinations of both. A single date variable stores a point in time (Date and Time) January 1, 0001 to December 31, 9999
Date
Sequences of text characters, e.g. names, addresses, paragraphs of a document, whole documents etc. Up to 2 billion characters
String
Stores a single alphanumeric character (e.g. the letter ‘x’,
- r a digit or punctuation
symbol 0 to 65535
Char
Used to store simple true/false, on/off, up/down or
- ther 2-state values
True or False
Boolean
Type Compatibility
Numeric values are interchangeable between
different types, but:
There can be a loss of data
E.g. Store a double value 3.14159268 in an Integer variable
(result is 3)
General principle
Store contents of lower precision data type in higher
precision or wider range variable – no problem
Store contents of higher precision or wider range data type in
lower precision variable – loss of data
Constant
A Constant is like a variable with a fixed value
(an oxymoron if ever there was one)
Constants are used in programs
to give memorable names to fundamental values
e.g. pi, Gravitational_Constant, cm_per_inch
to provide for values that might change from version
to version of a program
e.g. VATRate, max_class_size, database_server_name
to provide for data that may have one value in a test
version and another in the final release of the software
e.g. sample_size (could be 10 in tests, 1000 in working
s/ware), max_redials (could be 10 in tests, 3 in real life)
Literals
A Literal is a value stated explicitly in program
code
e.g. 3.1415927, 100, “Fred Bloggs”,
“www.our_site.com/data_server”
i.e. any value that could be applied to a variable
Generally, try to eliminate literals from program
code
Define constants to replace them Not feasible to get rid of them all, but should aim to
minimize the need to change statements throughout a program if some assumed value changes
Identifiers
Names given to:
Variables Constants Structures and Classes Class members (member variables, methods) Code routines Modules, Namespaces Forms, controls etc.
The purpose of an identifier is to allow a programmer to
manipulate some value in code:
E.g.
FullPrice = Price + Price * VATRate
FullPrice and Price are variables, referring to some stored value.
Need to use the names to access these values
Rules for Identifiers
Must start with alphabetic or underscore
e.g. Cust_Name, PI, _data_Server
Must not be an existing keyword in the
language:
e.g. avoid – Integer, Sub, For, Next, Function etc.
Must contain one or more alphabetics or
numerics
e.g. X, A2, _d, _5
Must be no more than 16383 characters long
Not much of a restriction
Assigning literal values - rules
Numeric assignments
State values directly. e.g. 12.3, 10000, -125
Char assignments (single characters)
Use double quotes. e.g. “X”, “;”
String assignments (text)
Use double quotes. e.g. “Fred”, “ABCEDFG”
Dates and times
Enclose in double quotes. e.g. “14/02/2003”, “17:00”,
“21/08/2003 12:30:00”
Initialising Variables
A variable with no value assigned will contain the type
equivalent of zero:
0 or 0.0 for numbers “” for strings and characters “01/01/0001 00:00:00” for dates/times
Typically a variable is declared then assigned
Can lead to a situation where a zero value is in the variable
unintentionally for some time
Can lead to subtle errors Programmer can declare and forget to assign at all
Possible to Declare/Assign in one step
e.g. Dim myName As String = "Alistair" Prevents problem of unassigned variables
Expressions
An Expression is some combination of:
Variable references Operators Constants or literals Function calls
Legal expressions always reduce to a single value
e.g. 12/3 + 4*2 – 11 (expression) evaluates to 1 (4 + 8 –
11)
e.g. Sqr(16) (expression) evaluates to 4 (sqr root of
16)
Operators
Generally, expressions involve
- perators
A range of legal operators is available
for the range of types
e.g. Numeric (+, -, *, /, ^, +=, *=) e.g. String (&, &=) e.g. Dates (+, -, +=, -=)
String Operators
Strings can be added together
Two operators (plus modifications) available for this - + and &
The + operator used with strings joins them together
e.g. “Fred “ + “Bloggs” = “Fred Bloggs”
The & operator used with any data type will produce a
string result
e.g. “Catch ” & 22 = “Catch 22” e.g. "Strange but " & (2 = 2)
Also the ‘increment’ variants of these operators
e.g. s = “Farenheit “
s &= 451
e.g. name = “Charlie “
name += “Brown”
Statements and sequence
Statements organised in a sequence will
- perate in that sequence
This is crucial – it provides the mechanism for
performing a task in a number of steps
Price = 10.00 TaxRate = 0.175 Price = Price + Price * TaxRate Console.WriteLine(Price) These must come before this for the calculation to work.
Blocks
A sequence of statements
that is not interrupted by control statements (later) is a block
Can be considered to be a
unit of code: a block of statements performs a task
Blocks can be given a
name (identifier) by enclosing them as a Sub
- r Function
This allows us to Call a
block of code by using its name as a statement
Sub AddTax() Price = 10.00 TaxRate = 0.175 Price = Price + Price * TaxRate Console.WriteLine(Price) End Sub Sub Main() AddTax() End Sub
Structured Variables
Individual variables are
adequate for storing data in programs, but by combining variables, we can provide better structural integrity
Allows us to create our own,
meaningful, variable types
Organising data like this is
beneficial in programming
Keep related info together Improves clarity Simplifies moving data around a
program Structure Rectangle Dim Length As Single Dim Width As Single End Structure Sub Main() Dim R As Rectangle R.Length = 12.5 R.Width = 16.2 ‘ ‘ ‘ ‘ ‘ ‘ End Sub
Enumerations
Some types of
information fit most comfortably into restricted ranges
e.g. Colours, Shoe Sizes,
Types of class (Lecture, Lab, Tutorial, Seminar) etc.
An enumeration is an
efficient way of creating a list of possible values for a type
Reduces possibility of
error by assigning an invalid value to a variable (only the specified range is available)
Enum MusicStyles Classical Swing Jazz Pop Rock Techno End Enum Enum ClassType Lecture Lab Tutorial Seminar End Enum
Summary
CLR is a Virtual Computer that .NET programs run on Variables used to store items of data (and provide a name for them)
Range of predefined types available
Literals and Constants are specific values used in expressions Operators are used to form expressions (with variables, literals and
constants) to do calculations
Strings (sequences of text characters) can also be calculated and
manipulated
A sequence of statements specifies the order a process will be
done in
A block of statements performs some task, and can be named (as a
Sub or Function) to make it easy to use
Individual data values can be organised into groups as structures,
- r limited in range by enumerations