3 the visual basic net language
play

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


  1. 3. The Visual Basic .NET Language Learning to Program

  2. Overview � The Common Language Runtime � Variables, data types, constants and literals � Identifiers � Expressions, operators � Statements and blocks � Structured variables and enumerations

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

  4. VB Running on a .NET Platform A Visual Basic program The .NET Common Language Runtime environment Computer hardware and Operating System

  5. 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 operating system used

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

  7. Example use of variables Sub Main() Length Dim length, width, area As Single length = 12.5 12.5 width = 8.75 Width area = length * width Console.WriteLine("Area is {0}", area) 18.5 End Sub Area � Variables can be used to 231.25 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

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

  9. Types in Detail Type Name Range Description -2,147,483,648 to 2,147,483,647 A 32 bit signed number with Integer no fractional part -9,223,372,036,854,775,808 to A 64 bit signed number with Long 9,223,372,036,854,775,807 no fractional part -3.402823E+38 to -1.401298E-45 for negative values; A single-precision floating- Single 1.401298E-45 to 3.402823E+38 for positive values point number stored in an efficient binary representation (7 digits of precision) -1.79769313486231E+308 to A double-precision floating Double -4.94065645841247E-324 for negative values; point number stored in 4.94065645841247E-324 to an efficient binary 1.79769313486231E+308 for positive values representation (15 digits of precision) +/-79,228,162,514,264,337,593,543,950,335 with no A high precision number Decimal decimal point; stored in a direct +/-7.9228162514264337593543950335 with 28 decimal representation places to the right of the decimal; smallest non-zero to improve the accuracy number is of calculations (29 digits +/-0.0000000000000000000000000001 of precision)

  10. Types in Detail II Type Name Range Description January 1, 0001 to December 31, 9999 Dates, times and Date combinations of both. A single date variable stores a point in time (Date and Time) True or False Used to store simple Boolean true/false, on/off, up/down or other 2-state values 0 to 65535 Stores a single alphanumeric Char character (e.g. the letter ‘x’, or a digit or punctuation symbol Up to 2 billion characters Sequences of text String characters, e.g. names, addresses, paragraphs of a document, whole documents etc.

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

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

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

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

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

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

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

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

  19. Operators � Generally, expressions involve operators � A range of legal operators is available for the range of types � e.g. Numeric (+, -, *, /, ^, +=, *=) � e.g. String (&, &=) � e.g. Dates (+, -, +=, -=)

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

  21. Statements and sequence � Statements organised in a sequence will operate 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 These must come before this for the Price = Price + Price * TaxRate calculation to work. Console.WriteLine(Price)

  22. Blocks � A sequence of statements Sub AddTax() that is not interrupted by control statements (later) Price = 10.00 is a block TaxRate = 0.175 � Can be considered to be a Price = Price + Price * TaxRate unit of code: a block of statements performs a Console.WriteLine(Price) task End Sub � Blocks can be given a name (identifier) by enclosing them as a Sub Sub Main() or Function � This allows us to Call a AddTax() block of code by using its End Sub name as a statement

Recommend


More recommend