You are on page 1of 29

CONTROLS

Numeric Data Types


Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
Long 4 bytes -2,147,483,648 to 2,147,483,648
-3.402823E+38 to -1.401298E-45 for negative
values
Single 4 bytes
1.401298E-45 to 3.402823E+38 for positive
values.
-1.79769313486232e+308 to -
4.94065645841247E-324 for negative values
Double 8 bytes
4.94065645841247E-324 to
1.79769313486232e+308 for positive values.
-922,337,203,685,477.5808 to
Currency 8 bytes
922,337,203,685,477.5807
+/- 79,228,162,514,264,337,593,543,950,335 if no
decimal is use
Decimal 12 bytes
+/- 7.9228162514264337593543950335 (28
decimal places).
Non-numeric Data Types
Type Storage Range
String(fixed length) Length of string 1 to 65,400 characters
String(variable length) Length + 10 bytes 0 to 2 billion characters
January 1, 100 to
Date 8 bytes
December 31, 9999
Boolean 2 bytes True or False
Object 4 bytes Any embedded object
Any value as large as
Variant(numeric) 16 bytes
Double
Same as variable-
Variant(text) Length+22 bytes
length string
Suffixes for Literals

Suffix Data Type


& Long
! Single
# Double
@ Currency
 In addition, we need to enclose string literals
within two quotations and date and time literals
within two # sign. Strings can contain any
characters, including numbers. The following
are few examples:
 memberName=”Turban, John.”
TelNumber=”1800-900-888-777″
LastDay=#31-Dec-00#
ExpTime=#12:00 am#
Variable Names

 Like the mailboxes, each variable must be


assigned a name. To name a variable in
Visual Basic 2017, you need to follow a set
of rules as follows:
 It must be less than 255 characters
 No spacing is allowed
 It must not begin with a number
 Period is not permitted
Valid Names Invalid Name
My_Name My.Name
VB2015 2015VB
LongName&Canbe&U
Long_Name_Can_be
se *& is not
USE
acceptable
Declaring Variables

 The syntax to declare a variable in Visual Basic 2017 is


as follows:
 Dim VariableName As DataType
 If you want to declare more variables, you can declare
them in separate lines or you may also combine more in
one line, separating each variable with a comma, as
follows:
 Dim VariableName1 As DataType1, VariableName2 As
DataType2, VariableName3 As DataType3
Private Sub Form1_Load(ByVal sender
As System.Object, ByVal e As
System.EventArgs) Handles
MyBase.LoadDim password As String
Dim MyName As String
Dim Num1 As Integer
Dim Num2 As Single
Dim Sum As Integer
Dim StartDate As Date
 Notice that you can assign a value to the
string in one line using the = sign instead
of declaring the variable and then give it
a value in another line.

 Dim YourName as String="George"


 is same as
 Dim YourName as String
 YourName="George"
Assigning
firstNumber=100 Values to Variables
 secondNumber=firstNumber-99
 userName=”John Lyan”
 userpass.Text = password
 Label1.Visible = True
 Command1.Visible = false
 Label4.text = textbox1.Text
 ThirdNumber = Val(usernum1.Text)
 total = firstNumber + secondNumber+ThirdNumber
 MeanScore% = SumScores% / NumSubjects%
 X=sqr (16)
 TrimString= Ltrim (“ Visual Basic”, 4)
 Num=Int(Rnd*6)+1
Scope of Declaration

 PrivateVariableName as Datatype
Static VariableName as Datatype
Public VariableName as Datatype
Constants

 Private Sub Button1_Click(sender As Object, e As


EventArgs) Handles Button1.Click
 Const Pi As Single = 3.142
 Dim R As Single = 10
 Dim AreaCircle As Single
 AreaCircle = Pi * R ^ 2
 MsgBox("Area of circle with " & "radius" & R & "=" &
AreaCircle)
 End Sub
ARITHMETIC
OPERATIONS
Mathematical
Operator Example
Function
+ Addition 1+2=3
– Subtraction 10-4=6
^ Exponential 3^2=9
* Multiplication 5*6=30
/ Division 21/7=3
Modulus(returns
the remainder of
Mod 15 Mod 4=3
an integer
division)
Integer
Division(discards
\ 19/4=4
the decimal
places)
Private Sub BtnCal_Click(ByVal sender As LblSum.Text=sum
System.Object, ByVal e As
LblDiff.Text=difference
System.EventArgs) Handles BtnCal.Click
LblPdct.Text = product
Dim num1, num2, difference, product,
quotient As Single LblQuo.Text = quotient
Dim num1 As Single, num2 As Single End Sub
Dim sum As Single, diff As Single, pdct As
Double, quo As Double
num1 = TxtNum1.Text
num2 = TxtNum2.Text
sum=num1+num2
difference=num1-num2
product = num1 * num2
quotient=num1/num2
Example 2
Private Sub BtnCal_Click(ByVal sender As Me.CreateGraphics
System.Object, ByVal e As
myGraphics.DrawPolygon(myPen,
System.EventArgs) Handles Button1.Click
myPoints)'Pythagoras equation
'To draw a triangle at runtime
Dim myPen As Pen
Dim a1, b1, c1 As Single
Dim A As New Point(10, 10)
a1 = TxtA.Text
Dim B As New Point(100, 50)
b1 = TxtB.Text
Dim C As New Point(60, 150)
c1 = (a1 ^ 2 + b1 ^ 2) ^ (1 / 2)
Dim myPoints As Point() = {A, B, C}
LblC.Text = c1
myPen = New Pen(Drawing.Color.Blue,
End Sub
5)

Dim myGraphics As Graphics =


Example 3

 Underweight = <18.5
Normal weight = 18.5-24.9
Overweight = 25-29.9
Obesity = BMI of 30 or greater
 BMI can be calculated using the formula weight/( height )^2
 where weight is measured in kg and height in meter. If you only know your
weight and height in lb and feet, then you need to convert them to the
metric system. The
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArsgs) Handles Button1.ClickDim height, weight, bmi As Single
height = TextBox1.Text
weight = TextBox2.Text
bmi = (weight) / (height ^ 2)
LblBMI.Text = bmi
End Sub
 Ruth
 BMI – w/h
 Area of a triangle – base/h
Area of a rectangle -
 Charmaine
 Area of a circle
Volume of a cylinder
Volume of a cone
 Nico
 Conversion of lb to kg
Conversion of Fahrenheit to Celsius
Conversion of mile to km
 Joshua
 Volume of a sphere
Mean
Variance
 Joram
 Compound interest
Future value

Nikolai
 Sum of angles in polygons
Conversion of meter to foot
 Reymark
 Conversion of meter to foot
 Mean
Variance
 LEAH
 Conversion of meter to foot
 Future value
 Mean

You might also like