8/8/2019 Chng 3- VB
1/22
Chng 3. ASP.NET v VB.NETS lc v VB.NETVariables v Arrays
Operators
Cc iu kin (conditional), looping v branching logic
Functions v Subroutines
Event Handler
Classes
Ving dng ch li thng dng
8/8/2019 Chng 3- VB
2/22
3.1 S lc v VB.NET
ASP.NET nm trong cu trc nn ca .NET framework v dng.NET programming language nh VB.NET (mt trong 25 NNLT .NEThin nay) pht trin trang Web => cn bit s lt v .NETframework cng VB.NET dng cho cc trang ASP.NET.
Nu thch, ta c th dng C# hay C++ thay v VB.NET, v s khcbit phn ln l v c php (syntax) ch nguyn tc v cu trclp trnh th nh nhau. D dng bt c NNLT no, khi bin dchvn phi qua ngn ng trung gian Intermediate Language (IL) vqun l bi CLR.
8/8/2019 Chng 3- VB
3/22
3.2 Variables v Arrays
Variables dng lu tr d kin (data) trong b nh (memory) camy tnh. Taphi khai bo (declare) bin s trc khi dng vi keyword Dim VD: DimmyVariable As String
.Data Types chia ra lm 5 nhm vi 10 kiu c bn gi l primitive types:
Byte Integers 1 byte (c bit nh System.Int)
Short Integers 2 bytes (System.Int16)
Integer Integers 4 bytes (System.Int32)
Long Integers 8 bytes (System.Int64)
Single decimal 4 bytes (System.Single)
Double decimal 8 bytes (System.Doublel)
Decimal decimal 12 bytes (System.Decimal)
Char String single Unicode character (System.Char)
Date Dates ngy gi (System.DateTime)
Boolean Boolean True/False (System.Boolean)
8/8/2019 Chng 3- VB
4/22
3.2 Variables v Arrays (tt)
Arrays l 1 tp hp cc bin s c lin h ring bit qua chs(index) ca Arrays.
Arrays dng trong VB.NET bt u vi index bng s 0.
Mi bin s trong Array phi cng kiu d liu (data type).
Array c khai bo nh v d sau :
DimmyArray(9) As Integer
Dim yourArray( ) As String = { "T", "Su", "Dn", "Mo",_ "Thn",
T", "Ng", "Mi", "Thn", "Du", "Tut", "Hi" }
8/8/2019 Chng 3- VB
5/22
3.4 Operators
^ M
+, - Du
*, / Php nhn, chia
\ Php chia ly phn d
+, - Php cng, trBitNot, BitAnd, BitOr, BitXor Bitwise NOT, AND, OR v XOR
&, + Ghp ni xu
=, , , >=,
8/8/2019 Chng 3- VB
6/22
3.5 Cc cu trc iu khin
If statements Tng cng 3 kiu c php nh sau: If (condition) Then
......
End If If (condition) Then
(your code for condition = True)...Else
(your code for condition = False)...
End If If (condition 1) Then
(your code for condition 1 = True)
...ElseIf (condition 2) Then
(your code for condition 2 = True)...
Else(ngoi ra, thi hnh code y)...
End If
8/8/2019 Chng 3- VB
7/22
8/8/2019 Chng 3- VB
8/22
3.5 Cc cu trc iu khin (tt)
While Loops
Rt tin li trong trng hp ta khng bit trc phi lp i lp li cng vicbao nhiu ln. Thi hnh cho n khi iu kin nh trc tr thnh False. Cphp ca While loop nh sau:
While condition
Your Code
End While
Th d ta mun b tr 1 my m (counter) t 1 n 9 v hin th (display) ktqu bng s browser:
Dim intCounter As Integer = 1
While intCounter < 10
Response.Write(intCounter & "
")
intCounter += 1
End While
8/8/2019 Chng 3- VB
9/22
3.5 Cc cu trc iu khin (tt)
Do Loops
Do loop cng tng t nh while loop, chkhc ch Do loop thihnh cng vic trc ri mi kim tra iu kin xem c ph hpkhng?
Dim intCounter As Integer = 1
Do
Response.Write(intCounter & "
")
intCounter += 1
Loop While intCounter < 10
8/8/2019 Chng 3- VB
10/22
3.5 Cc cu trc iu khin (tt)
For Loops Ta dng For loop khi bit trc s lp i lp li vic thi hnh ngunm bao
nhiu ln. My m trong trng hp ny (t ng tng hay gim ty theocch b tr) s thng bo chnh loop ca n khi no chm dt. C php nhsau:
For intCounter = 1 to 10
Response.Write(intCounter & "
")Next hoc l:
For intCounter = 10 to 1 Step -1Response.Write(intCounter &
"
")
Next
For loop cn c 1 dng khc l For each ... loop, thng dng vn dng ccthnh phn (hay yu t - elements) trong 1 b su tp (collection) t nh Arraychng hn:
Dim arrayWeekDays( ) As String = {"Mon", "Tue", "Wed", "Thu", "Fri"} For eachstrDay in arrayWeekDays
Response.Write(strDay & "
")Next
8/8/2019 Chng 3- VB
11/22
3.5 Cc cu trc iu khin (tt)
Nu tamun nhy ra khi loop trc khi loop chm dt, ta c th dngkeyword Exit Do (trong Do loop) hay Exit For (trong For loop), VD:
Dim intCounter As Integer = 1
DoResponse.Write(intCounter & "
")
intCounter += 1If intCounter = 8 then
Exit DoEnd If
Loop While intCounter < 10
hay
For intCounter = 1 to 10Response.Write(intCounter & "
")If intCounter = 8 then
Exit ForEnd If
Next
8/8/2019 Chng 3- VB
12/22
3.6 Functions
Functions
C php nh sau:
Function FunctionName (Parameter1 As Type, ... , ParameterN As
Type) As ReturnType
'you code here
Return ReturnValue
End Function
Vi VB.NET Function tin dng:
8/8/2019 Chng 3- VB
13/22
3.6 Functions (tt)
Cc functions v Date v Time Function Phn din t cng dng
day(datetime) Tr li 1 s t 1 ti 31 biu th ngy trong thng.
dateDiff(dateinterval, date1,date2) Tr li 1 s din t khong thi gian cch bitgia 2 ngy: (date1) v (date2). S ny c th di dng yyyy (year), q (quarter),m
(m
onth ), y (year), d (day), h(hour), n (m
inute) hay s (second).hour(time) gi trong ngy.
isdate(datetime) datetime c phi l 1 ngy hp l khng?
minute(time) pht trong gi.
month(datetime) thng trong datetime.
now( ) Tr li ngy gi hin thi
second(time) 0 ti 59 biu th giy trong pht.
year(datetime) nm ca datetime (c gi tr t 1 ti 9999)
8/8/2019 Chng 3- VB
14/22
3.6 Functions (tt)
Cc Functions v Ton HcFunction Phn din t cng dng
abs(value) Tr li gi tr tuyt i (absolute) ca value.
atan(value) Tr li s arctangent ca value.
cos(value) Tr li s cosin ca value.
exp(value) Tr li s exponent ca value.
fix(value) Tr li phn integer ca value :fix(5.8)=5, fix(-5.8)=-6
hex(value) Tr li s hexadecimal ca value (base 10 to base 16).
int(value) Tr li phn integer ca value : in(5.8)=5, int(-5.8)=5
log(value) Tr li s nature logarithm ca value.
oct(value) Tr li s octal ca value (base 10 to base 8).
rnd Tr li s ngu nhin.round(value [, dec]) Lm trn vi 0, hoc dec s thp phn ca value.
sin(value) Tr li s sin ca value.
sqrt(value) Tr li s cn 2 ca value.
tan(value) Tr li s tangent ca value
8/8/2019 Chng 3- VB
15/22
3.6 Functions (tt)
Cc Functions StringsFunction Phn din t cng dng
instr([start, ]s1, s2[, compare]) Tr li s biu th v tr 1 ca s2 trong s1,ngoi ra tr li s 0. Compare c th di dng 0 (=BinaryCompare) hay 1(=TextCompare).
left(s, len) Tr li xu con vi di =len k t pha bn tri ca s.len(s | variable) Tr li chiu di ca string hay tng s bytesm variable chang.
mid(s, start[, len]) Tr li xu con vi di len, k t v tr th start cas.
replace(exp, find, replace[,start[, count[, compare]]]) Thay th gi tr find trongbiu thc expression bng gi tr replace bt u t v tr start vi s ln thayth count. Lu gi trmc nh ca count l -1 - ngha l thay th tt c g truytm c, compare dng di dng 0 hay 1 nh trong phn instr.
right(s, len) Tr li xu con 1 di = len bt u t pha bn phi s.
8/8/2019 Chng 3- VB
16/22
3.7 Th tc : Subroutines
Subroutines Th tc C php nh sau:
Sub SubroutineName (Parameter1 As Type, ... ,ParameterN As Type)
'you code here
End Sub
V d subroutine Page_Load rt ph thng trong cc trang ASP.NET :
Sub Page_Load (Obj As object, e As eventargs)
'you code here
End Sub
Cc bin s (variables) trong ( ) gi l thng s (parameters hay arguments),dng truyn cc gi tr u vo cho th tc
8/8/2019 Chng 3- VB
17/22
3.7 Th tc : Subroutines
Event Handler
Ny , ta thy 1 trong nhng thng s (parameters) s dng trong th tctrn l thng s v s c (EventArgs). Trong cc trang Web ca ASP.NET , sc c th xy ra bt c lc no, nh user nhpmi chut vo 1 nt no trnForm hoc nhp vo 1 hnh nh, do ta cn chun b v to ra ci gi l event
handler c th p ng li cc s c . C php ca event handler gingnh c php ca subroutine, s khc bit l nm parameters list vi thngs chring cho loi EventArgs. Khi 1 s c khi ng, s c s to ra ccbin s nhm din t vic g xy ra v event handler s da trn cc bins phn ng sao cho thch hp.
.NET t ng gip ta to khung cho cc th tc x l s kin ( vi tn th tc,ds tham s v dong end sub, phn thn th tc, ta phi t vit code )
8/8/2019 Chng 3- VB
18/22
3.8 Classes
Classes l phn ta xc nh hay nh ngha cc i tng (Object) t nh nh ngha 1 ci ng h, ta din t kim gi, kim pht, kim giy cng cc con schgi, cch b tr gi gic hay ngy thng nm, ... Tng t nh th, classnh ngha i tng (Object) qua cc c tnh (properties) v cc phngphp (method) biu th c trng cho class.
Ta nn nh r 1 iu: 'Mi th trong .NET Framework hay VB.NET u i biucho classes'.
C php nh sau:
Class classname
properties
subroutines
functions
End Class
Trc khi dng, nh instantiate class ra 1 i tng (Object).
8/8/2019 Chng 3- VB
19/22
3.8 Classes (tt)
C rt nhiu Base Classes trong .NET Framework, tng qut c th xp vonhng loi sau:
String Collections v Arrays: VD Arrays, Lists, Maps, Linked Lists, ...
WinFor ms: dng hin th (display) Windows v cc Controls VD Text Boxes,Combo Boxes, List Boxes, File Dialogs, ...
Web For ms: phc tho dng chomng, ta s nghin cu k sau.
File Handling: dng lt qua li (navigate) cc file system trong my hay trongmng, kim tra c tnh (properties) ca files, read, modify hay write cng nhchuyn (move) v sao chp (copy) cc tp tin hay folders.
Registrry Access: lt qua li, c hay vit ni dung ca registry.
Internet: ni vo mng, ti ln hay ti xung cc tp tin.
ADO.NET: ni vo cc c s d liu (database) v vn dng cc records vi 1khi nimmi v disconnected data cng nh s dng XML chuyn data ikhpmi nimi ch.
8/8/2019 Chng 3- VB
20/22
3.9 Inheritance
Inheritance nm vai tr quan trng trong classes v OOP. Takhng cn phi to ra 1 class mi hon ton khi c 1 classtng t nh ci ta mun m chcn to ra 1 lp mi , k that lp c (base class). Tr li trng hp ci ng h, nu cth to 1 class gi l Clock, ri sau c th xy dng class mi ly
Clock class lm c s (base class) v thm vo nhng g ctrng n hay lin h n loi ca Clock l Analog hay Digital.
C php nh sau vi th d v AnalogClock:
Class AnalogClock: Inherit Clock
private ClockWound ASP.NET Boolean = FalseSub WindClock( )
ClockWound = TrueEnd Sub
End Class
8/8/2019 Chng 3- VB
21/22
V d : XD trang web nh sau :
8/8/2019 Chng 3- VB
22/22
Code view
Protected Sub Button1_Click(ByVal sender As Object, ByVal e AsSystem.EventArgs) Handles Button1.Click
kq.Text = "Php cng: " & so1.Text & " + " & so2.Text & " = " &CInt(so1.Text) + CInt(so2.Text)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click
kq.Text = "Php tr: " & so1.Text & " - " & so2.Text & " = " &CInt(so1.Text) - CInt(so2.Text)
End Sub
. .