Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types, Arithmetic Operations

Similar presentations


Presentation on theme: "Data Types, Arithmetic Operations"— Presentation transcript:

1 Data Types, Arithmetic Operations
Lecture 2

2 What are we going to cover
Variables Data types Operators Methods Classes and objects

3 Variable 18 Address: 0803 Name: MyAge Address: 0000 A variable is a symbolic name for a location in memory where some data are stored. Every variable has its address in memory and value

4 Declaring variable in VB.NET
Dim n As Integer Dim p As Double Dim str As String Dim weLikeFunPro As Boolean Name: n 0.0 Name: p Name: str

5 Variable identifiers You can choose any name for variables as long as
the name is not already a word in the VB.NET language (such as class, imports); the name has no spaces in it; the name does not include operators such as “+” and “-”; the name starts either with a letter or an underscore (_). Valid names: correct, _correct Invalid names: #wrong, ^wrong, 1wrong Note: control names are variables, rules apply

6 Data Type Every variable has its type identifying what sort of data should be stored in it: Integer, String, Floating-point number etc.

7 Data Type It is very essential to show the type of the variable
Because internally the computer stores all the values in binary and does not differentiate the type. The compiler needs to know the type in order to be able to convert the binary value into the specified type. It also prevents logical errors e.g. if you try to assign some text into a numeric variable compiler would flag this as error.

8 Data Type Integer: 70 Char: ‘ F ‘ Double: 70.0

9 Variable types (integer types)
VB.NET name Description Range Byte 8-bit unsigned number 0 to 255 Short 16-bit signed number to 32767 Integer 32-bit signed number -2^31 to 2^31-1 Long 64-bit signed number -2^63 to 2^63-1

10 Variable types (decimal types)
VB.NET name Description Precision Range Single 32-bit floating point number 7 significant digits +/- 1.4x10^(-45) to +/- 3.4x10^38 Double 64-bit floating point number 15-16 significant digits +/- 5.0x10^(-324) to +/- 1.7x10^308 Decimal 128-bit fixed point number 28 significant digits +/- 1.0x10^(-28) to +/- 7.9x10^28

11 Variable types (other)
Char – one letter String – text of any length Date – date and time Boolean – true or false

12 Conversion between types
Generally compiler should not let you to assign one data type to another However in practice it uses implicit conversion for the types it knows how to convert e.g. Integer to Long In VB.Net compiler is even more flexible allowing you to avoid some of explicit conversion required Option Strict On will enable strict rules

13 Conversion between types
CType(value, class) Dim a As Long = 1000 Dim b As Integer b = CType(a, Integer) CDbl CInt

14 Conversion between types
All .NET languages – Convert class: Convert.ToBoolean() Convert.ToDateTime() Convert.ToDecimal() Convert.ToDouble() Convert.ToInt32() - Integer Convert.ToInt64() - Long

15 Assignment operation Operator ‘=‘ represents the assignment operation which means: to assign the value of the expression on the right hand side of the ‘=‘ sign to the variable on the left hand side n = 5 p = 2.6 Str = “Hello World” weLikeFunPro = True 5 Name: n

16 Arithmetic operations
VB.Net supports all simple arithmetic operations: +, - , *, /, \ - integer division, Mod – remainder division, ^ - exponentiation, & - String concatenations

17 Arithmetic operations
P = (n * 3 + 2) mod 5 P = N / M N = 5 ^ 3

18 Operator Precedence Highest Exponentiation (^) * / \ Mod + - Lowest

19 Constants Constants are the variables whose values cannot be changed by the program Declaring the constants: Const PI As Single = 3.14 Const PASSWORD As String = “magic”

20 Comments Comments are very useful tool that help the developers to put hints on the code blocks for future use. Use single quote to point the comment string ‘ This is a comment line Always use comments in your programs. This will make your life easier.

21 String functions String class exposes a variety of methods to manipulate strings String.Insert String.ToLower, String.ToUpper String.Substring String.Split String.Join and many more…

22 Methods A method is a block of code. A method has a header and a body.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click n = n + 1 p = p – 0.1 End Sub

23 Methods Methods are used to break up the code from one monolithic peace into smaller reusable modules. It is like breaking a big job into a smaller operations to be fulfilled by the different people We will learn about functions and subroutines later

24 Classes and Objects For now you can think of Classes as of Data Types and Objects as of Variables

25 Object properties and methods
Almost all objects have properties and methods. To access to the object’s properties or methods use dot sign: Label1.Text = “Hello World” Objects can have a number of variables (properties), methods and other objects.

26 The Windows components
You can access the variables of the windows components at the design time using Properties Window The methods and as well as properties of an object are also accessible in the code view:

27 The End


Download ppt "Data Types, Arithmetic Operations"

Similar presentations


Ads by Google