VB .NET Revisit 1 VB .NET Revisit
VB .NET Revisit 1
VB .NET Revisit
VB .NET Revisit 2
Software Development Life Cycle
• Problem Definition
• Requirement Analysis
• Systems Design
• Implementation
• Testing & Debugging
• Deployment
• Maintenance
VB .NET Revisit 3
VB .NET
• .NET CLR-compliant language• Event-driven• Object Oriented• Visual Studio .NET
– A Rapid Application Development (RAD) environment
– An Integrated Development Environment (IDE)
• Editor, Command Line Compiler
VB .NET Revisit 4
Building HelloWorld with Visual Studio .NET
VB .NET Revisit 5
Hello World - Console
• A Console application• To compile (by command line compiler):
– vbc HelloWorld.vb
• To execute:– HelloWorld.exe
Imports System ‘ import System namespace
Public Module Hello ‘ a Standard Module Public Sub Main()
Console.WriteLine("Hello World!") End SubEnd Module
VB .NET Revisit 6
Hello World - GUIImports System ' import System namespaceImports System.DrawingImports System.Windows.Forms
Public Class HelloWindows Inherits Form ' inherits System.Windows.Forms.Form
Private lblHelloWindows As Label
Public Shared Sub Main()Application.Run(New HelloWindows())
End Sub
Public Sub New() ' constructorlblHelloWindows = New Label()With lblHelloWindows .Location = New Point(37,31) .Size= New Size(392,64) .Font = New Font("Arial",36) .Text = "Hello, Windows!" .TabIndex = 0 .TextAlign = ContentAlignment.TopCenterEnd With
Me.Text = "Visual Basic .NET"AutoScaleBaseSize = New Size(5, 13)FormBorderStyle = FormBorderStyle.FixedSingleClientSize = New Size(466,127)
Controls.Add(lblHelloWindows) End SubEnd Class
VB .NET Revisit 7
Hello World - GUI
• A Windows Application
• To compile:– vbc HelloWorld2.vb /reference:System.dll,Syst
em.Drawing.dll,System.Windows.Forms.dll /target:winexe
• To execute:– HelloWorld2.exe
VB .NET Revisit 8
Class
Public Class Student ‘ a base class…
End Class
Public Class FtStudentInherits Student…
End Class
Dim s1 As New Student()Dim s2 As New FtStudent()s1 = s2
VB .NET Revisit 9
Access Modifiers
• Control the accessibility of types and types members (including fields, methods, etc.)
• Private: accessible only from within the context in which it is declared• Protected: from within the program in which it is defined and the derived
classes• Public: publicly accessible
Public Class SomeClass
Public Sub DoSomething()
End Sub
Private Sub InternalHelperSub()
End Sub
End Class
• Control the accessibility of types and types members (including fields, methods, etc.)
VB .NET Revisit 10
Fundamental Types
• Boolean• Byte• Short• Integer• Long• Decimal• Single• Double• Char• String• Date• Object
VB .NET Revisit 11
Variables, Literals
• Dim bFlag As Boolean = False• Dim datToday As Date = #01/01/2005#• Dim iValue As Integer• Dim lValue As Long• Dim shValue As Short = 128• Dim sngValue As Single• Dim dblValue As Double• Dim MyString As String
VB .NET Revisit 12
Type Conversion• Implicit Type Conversion:
– Char -> String– Byte -> Short -> Integer -> Long -> Decimal -> Single ->
Double• Explicit Type Conversion (Casting):
– CByte()– CShort()– CInt()– CLng()– CDec()– CSng()– CDbl()– CBool()– CChar()– CStr()– CDate()
VB .NET Revisit 13
Assignment Statements
Dim a, b As SomeClass
a = New SomeClass()
a.MyPublicMember = “value in a”
b = a
b = New SomeClass()
VB .NET Revisit 14
Operators
• +, -, *, /• \ (integer division)• Mod (modulo), 9 Mod 2• ^ (exponentiation), X^2• =, <>, <, <=, >, >=, AND, OR, NOT, XOR• TypeOf …Is, TypeOf(x) Is String• Is, s1 Is s2• Like, s1 Like "a*“
VB .NET Revisit 15
Basic I/OModule Module1
Sub Main() Dim n As Integer Dim s As String Console.Out.WriteLine("HelloWorld! What's your name?") s = Console.In.ReadLine() ‘ read in a string Console.Out.WriteLine("How old are you?") n = Console.In.ReadLine() ‘ read in a number Console.Out.WriteLine("Hi " + s + ", you're " + CStr(n) + " already.") ‘ or Console.Out.WriteLine("Hi {0}, you're {1} already.", s, n) Console.In.ReadLine() ‘ wait to press Enter End Sub
End Module
VB .NET Revisit 16
Branching Statements (1)• Call SomeMethod() or• SomeMethod()• Exit Do, Exit For, Exit Function, Exit Sub, Exit Try
If A = B Then statementsEnd If
If A = B Then statementsElse statementsEnd If
If A = B Then statementsElseIf expression Then statementsElse statementsEnd If
VB .NET Revisit 17
Branching Statements (2)
Select Case strColor Case “red”
… Case “green”, “blue”
… Case Else
…End Select
VB .NET Revisit 18
LoopDo While I < 10
…Loop
Do…
Loop Until I >= 10
For i = 0 to 4 Step 2Console.WriteLine(i)
Next
For Each i in iArrary ‘ iArrary is an array…
Next
For Each obj In col ‘ col is a collectionConsole.WriteLine(obj.ToString())
Next
VB .NET Revisit 19
Array
Dim a(4) As IntegerFor i = 0 to 4
Console.WriteLine(a(i))Next
Dim a() As Integera = New Integer(2) {}a(0) = 1a(1) = 2
Dim a() As String = {“First”, “Second”, “Third”}Dim a(5,10) As Integer
VB .NET Revisit 20
EnumerationModule Module1 Enum CardSuit clubs = 0 diamonds = 1 hearts = 2 spades = 3 End Enum Sub Main() Dim cs As CardSuit = CardSuit.hearts System.Console.Out.WriteLine("The value cs is {0}", cs) End Sub End Module
VB .NET Revisit 21
StructureModule Module1 Structure PixelCoord Public x As Single Public y As Single End Structure Sub Main()
Dim p As PixelCoord
p.x = 200 p.y = 100 System.Console.Out.WriteLine("The value p.x is {0}", p.x) System.Console.Out.WriteLine("The value p.y is {0}", p.y) End Sub End Module
VB .NET Revisit 22
CollectionDim col As New Collection()col.Add(“First item”)col.Add(“Second item”)col.Add(“Third item”)
Dim obj As ObjectFor Each obj In col
Console.WriteLine(CType(obj, String))Next
Dim i As IntegerFor i = 1 to col.Count
Console.WriteLine(CType(col(i), String))Next
VB .NET Revisit 23
Sub & Function
Sub CheckInput()
…
End Sub
Function Square(ByVal i as Integer) As Integer
…
Return x
End Function