Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

VBdot NET

The document provides an overview of Visual Basic .NET including data types, operators, statements, arrays, classes, properties, delegates, events, namespaces, constants, variables, enumerations, conditional statements, loop statements, procedures, functions, and exceptions.

Uploaded by

patchiwaka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

VBdot NET

The document provides an overview of Visual Basic .NET including data types, operators, statements, arrays, classes, properties, delegates, events, namespaces, constants, variables, enumerations, conditional statements, loop statements, procedures, functions, and exceptions.

Uploaded by

patchiwaka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Gliwice, March 2004

Agenda
 Data ty pes and operators
Visual Basic .NET  Statements
 A rray s
Short Ov erv iew  C lasses and objects
 Properties and indexers
 Delegates and ev ents

Marek Mittmann
2

First program Console application


' Hello world application Imports System
Module Hello Module Hello
Sub Main() Sub Main(ByVal CmdArgs() As String)
MsgBox("Hello world!") Dim I As Integer
End Sub Console.WriteLine("Hello world!")
End Module For I = 0 To CmdArgs.GetUpperBound(0)
Console.WriteLine("CmdArgs[{0}] = {1}", _
I, CmdArgs(I))
Next I
End Sub
End Module

3 4

Value types and reference


VB.NET data types types
Object refer enc e to object Dim i As Integer 'value type
String sequence of Unicode characters Dim s As String 'reference type
Char Unicode character i = 123
Byte 8-bit integer value s = "Text..."
Short 16-bit integer value
Integer 32-bit integer value stack heap
Long 64-bit integer value
Single floating-point number (single precision) i 123
Double floating-point number (double precision)
Boolean logical value ( True or False )
Decimal fixed-point decimal number
s Text…
Date date value
5 6

1
Boxing Operators
Dim i As Integer = 123
Dim o As Object Arithmetic + - * / \ Mod ^
o = i Logical/bitwise Not And Or Xor AndAlso OrElse
Concatenation & +
stack heap Comparison = <> < > <= >= Like Is
i 123 Assignment = += -= *= /= \= ^= &=
Miscellaneous AddressOf GetType
o System.Int32
123

7 8

Namespaces Constants and variables


Namespace MyNamespace
Namespace Inner Sub Main()
Class MyClass
' variables
Shared Sub Proc1()
Dim r As Single = 1.25F
'procedure body
Dim a, b As Integer
End Sub
Imports System, Microsoft.VisualBasic Dim c As Integer = 2
End Class
Imports NamespaceAlias = MyNamespace.Inner ' constants
End Namespace
Const pi As Single = 3.14F
End Namespace
Module MyApp
Sub Main() a = 12 : b = 1
NamespaceAlias.Proc1() Console.WriteLine(a + b + c)
End Sub Console.WriteLine(pi * r * r)
End Module
End Sub
9 10

Enumerations Conditional statements


Enum Color Sub Main(ByVal CmdArgs() As String)
Red ' = 0
Green = 10 ' = 10 If CmdArgs.Length = 0
Blue ' = 11 Console.WriteLine("No arguments")
End Enum ElseIf CmdArgs.Length = 1
Console.WriteLine("Single argument")
Sub Main() Else
Console.WriteLine("{0} arguments", _
Dim c As Color = Color.Green CmdArgs.Length)
DrawBox2D(10, 20, c) End If
DrawBox2D(12, 10, Color.Blue)
End Sub
End Sub

11 12

2
Select statement Loop statements
Sub Main(ByVal CmdArgs() As String) Dim I As Integer
For I = 0 To CmdArgs.Length-1
Console.WriteLine(CmdArgs(I))
Select Case CmdArgs.Length
Case 1, 2 Next I
Console.WriteLine("To few arguments")
Case 3 To 5 I = 0
While I < CmdArgs.Length
UseArgs1(CmdArgs)
Case Is < 7 Console.WriteLine(CmdArgs(I)) : I += 1
UseArgs2(CmdArgs) End While
Case Else
I = 0
Console.WriteLine("Error")
End Select Do
Console.WriteLine(CmdArgs(I)) : I += 1
End Sub Loop Until I >= CmdArgs.Length
13 14

Foreach statement Exit statement


Sub Main() Sub Main(ByVal CmdArgs() As String)
Dim I As Integer
Dim Table() As Integer = New Integer() {2, 1, -5}
Dim I As Integer For I = 0 To CmdArgs.GetUpperBound(0)
If CmdArgs(I) = "end" Then Exit For
For Each I In Table Console.WriteLine(CmdArgs(I))
Console.WriteLine(I) Next I
Next I
I = 0
End Sub While I < CmdArgs.Length
If CmdArgs(I) = "end" Then Exit While
Console.WriteLine(CmdArgs(I))
End While
End Sub
15 16

Procedures and functions Exceptions


Function Div(ByVal A As Integer, _
' Function ByVal B As Integer) As Integer
Function Sum(ByVal A As Double, _ If B = 0 Then Throw New Exception("Divide by zero")
ByVal B As Double) As Double Div = A / B
Sum = a + b End Function
End Function
Sub Main(ByVal CmdArgs() As String)
' Procedure Try
Sub Main(ByVal CmdArgs() As String) Console.WriteLine(Div(Val(CmdArgs(0)), _
If CmdArgs.Length < 2 Then Exit Sub Val(CmdArgs(1))))
Catch Ex As Exception
Console.WriteLine(Sum(Val(CmdArgs(0)), _ Console.WriteLine("Error: " & Ex.Message)
Val(CmdArgs(1)))) Finally
End Sub Console.Writeline("Done")
End Try
17 End Sub 18

3
Arrays Jagged arrays
' Single-dimensional arrays
Dim V1(10) As Integer Dim T1()() As Byte = {New Byte(2){}, New Byte(4){}}
Dim V2() As Integer = {2, 3, 0, 7, 3, -5}
Dim V3() As Integer = New Integer(2) {} Dim T2(1)() As Integer
T2(0) = New Integer(1) {1, -5}
Dim V4() As Integer = New Integer() {1, -2, 3, 0}
T2(1) = New Integer(2) {}
V1(0) = V2(2) T2(1)(0) = 2 : T2(1)(1) = -3 : T2(1)(2) = 7
V3(1) = 5
Dim I, J As Integer
' Multidimensional arrays For I = 0 To T2.Length-1
Dim M1(4, 3) As Integer For J = 0 To T2(I).Length-1
Dim M2(,) As Byte = {{1, 4}, {5, 7}} Console.WriteLine("[{0}][{1}] = {2}", _
I, J, T2(I)(J))
Dim M3(,,) As Byte = New Byte(3, 3, 2) {}
Next J
M2(0, 0) = 3 Next I
19 20

Using arrays Strings


Dim Arr(20), Arr2(20), I1, I2, I3 As Integer Dim S1 As String = "Alice has a cat"
Dim S2 As String = "line 1"&Chr(10)&Chr(13)&"line 2"
' Reversing
Array.Reverse(Arr) ' String length
Dim Len As Integer
' Sorting Len = S1.Length
Array.Sort(Arr)
' Concatenation
' Searching S1 = S1 & " and a dog"
I1 = Array.IndexOf(Arr, 5)
I2 = Array.IndexOf(Arr, 5, I1 + 1) ' Indexing
I3 = Array.BinarySearch(Arr, 10) Dim I As Integer
For I = 0 To S1.Length-1
' Copying Console.WriteLine("Char {0}: {1}", I, S1.Chars(I))
Arr.CopyTo(Arr2, 0) Next I
21 22

String manipulation Classes


Dim S1, S2 As String Class Point
Dim Cmp1, Cmp2, Cmp3 As Boolean Public X As Short = 0 ' attributes
Dim I1 As Integer Public Y As Short = 0 '
S1 = "Text..." : S2 = "text..."
' Case-sensitive comparison Public Sub New(ByVal X As Short, ByVal Y As Short)
Cmp1 = s1 = s2 Me.X = X : Me.Y = Y ' constructor
Cmp2 = String.Compare(S1, S2) = 0 End Sub
' Case-insensitive comparison
Cmp3 = String.Compare(s1, s2, true) = 0 Public Sub Show() ' member method
' Searching for a substring Console.WriteLine("({0}, {1})", X, Y)
I1 = S1.IndexOf("some text") End Sub
' Copying of a substring End Class
S2 = S1.Substring(2, 4)
' Replacing Sub Main()
S1 = S1.Replace("old", "new") 'string is immutable Dim Pt As Point = New Point(10, 5) : Pt.Show()
23 End Sub 24

4
With statement Passing arguments
Class Point
Class Point3D Dim X As Short = 0 : Dim Y As Short = 0
Public X As Integer = 0 ' passing arguments by value
Public Y As Integer = 0 Public Sub SetXY(ByVal X As Short, ByVal Y As Short)
Public Z As Integer = 0 Me.X = X : Me.Y = Y
End Class End Sub
' passing arguments by reference
Sub Main() Public Sub GetXY(ByRef X As Short, ByRef Y As Short)
Dim Pt As Point3D = New Point3D() X = Me.X : Y = Me.Y
With Pt End Sub
.X = 10 End Class
.Y = -2 ' ...
.Z = 4 Dim Pt As Point = New Point()
End With Dim X0, X1, Y1 As Short : X0 = 5 : X1 = 0 : Y1 = 0
End Sub Pt.SetXY(X0, 4)
25 Pt.GetXY(X1, Y1) ' after call X1 = 5 and Y1 = 4 26

Optional arguments Overloading


Class Point
Class Computer
Public X As Short = 0
Dim Id As String
Public Y As Short = 0
Dim Type As String

Overloads Public Sub SetXY(ByVal X As Short, _


Public Sub New(ByVal aId As String,
ByVal Y As Short)
Optional ByVal aType As String = "PC")
Me.X = X
Id = aId : Type = aType
Me.Y = Y
End Sub
End Sub
End Class

Overloads Public Sub SetXY(ByRef Pt As Point)


' ...
X = Pt.X
Y = Pt.Y
Dim C1 As Computer = New Computer("C001") ' Type = "PC"
End Sub
Dim C2 As Computer = New Computer("C002", "Notebook")
End Class
27 28

Inheritence Virtual methods


' Derived class Class Point
' Base class Class GraphObject
Inherits GraphObject
Class Point
Class GraphObject
Inherits GraphObject Public
Public X AsName As = String
Short 0
Public
Public Name
X As As String
Short = 0 : Public Y As Short = 0 Public Y As Short = 0
Public Sub New(ByVal Name As String)
PublicSubSub New(ByVal Name
As As String) PublicMe.Name = Name Name As String, _
Sub New(ByVal
Public New(ByVal Name String, _
Me.Name
ByVal = Name ByVal Y As Short)
X As Short, End X Sub
ByVal As Short, ByVal Y As Short)
End Sub
MyBase.New(Name) MyBase.New(Name)
EndMe.X
Class ' virtual
Me.X = X : method
Me.Y = in Y base class
= X : Me.Y = Y
End Sub End Public
Sub Overridable Sub Show()
Console.WriteLine("{0}", Name)
Public Sub Show() ' virtual
End Submethod in derived class
End Class
Public Overrides Sub Show()
Console.WriteLine("({0}, {1})", X, Y)
End Sub Console.WriteLine("{0}:({1}, {2})", Name, X, Y)
End Class End Sub
29 End Class 30

5
Abstract classes Interfaces
Interface IGraphObject
' abstract class Sub Show()
MustInherit Class GraphObject End Interface
Public Name As String
Class Point
Public Sub New(ByVal Name As String) Implements IGraphObject
Me.Name = Name '...
End Sub Public Sub Show() Implements IGraphObject.Show
Console.WriteLine("({0}, {1})", X, Y)
' pure-virtual method End Sub
Public MustOverride Sub Show() End Class
End Class
Dim Pt As Point = New Point(2, 5)
Dim graphObj As IGraphObject = Pt
If Not graphObj Is Nothing Then graphObj.Show()
31 32

Members accessibility Constructor and destructor


 Accessibility modifiers for classes Class ResourceWrapper
 Friend – accessible from the same module Dim Handle As Integer = 0

 Public – accessible from anywhere ' Constructor


Public Sub New()
 Accessibility modifiers for class members Handle = GetWindowsResource()
 Public – accessible from anywhere End Sub
 Protected – accessible from the same class and ' Destructor
from inherited classes Protected Overrides Sub Finalize()
 Private – only from within the same class ' Doesn't known, when it will be called
 Friend – from the same module FreeWindowsResource(Handle)
 Protected Friend – from the same module and MyBase.Finalize()
from inherited classes End Sub
33 End Class 34

Interface ID isposable Shared members


Class ResourceWrapper : Implements IDisposable
' ... Class GraphObject
Private Sub DoDispose() Shared Counter As Integer = 0
FreeWindowsResource(Handle) Public Name As String
Handle = 0
End Sub Public Sub New()
Counter += 1
Public Sub Dispose() Implements IDisposable.Dispose Me.Name = "GraphObject" + Counter.ToString()
DoDispose() End Sub
GC.SuppressFinalize(Me)
End Sub Public Shared Sub ResetCounter()
Counter = 0
Protected Overrides Sub Finalize() End Sub
DoDispose() End Class
End Sub
End Class 35 36

6
Properties Default properties
Class Worksheet
Class Point Dim Data(20, 20) As Double
Dim X As Short = 0
Dim Y As Short = 0 Default Public Property Value(ByVal Col As String, _
ByVal Row As Integer) As Double
Public Property X() As Short Get
Get Return Data(Row, ColToIndex(Col))
Return X End Get
End Get Set(ByVal Value As Double)
Set(ByVal Value As Short) Data(Row, ColToIndex(Col)) = Value
X = Value End Set
End Set End Property
End Property End Class
End Class ' ...
Dim sheet As Worksheet = New Worksheet()
37 Sheet("A", 10) = 20.5 38

Delegates Events
Class Button
Delegate Sub MyDelegate(ByVal Arg As String)
Public Event ClickEvent() ' points the event
Public Sub PerformClick() ' raises the event
Class Tester
RaiseEvent ClickEvent()
Sub Proc(ByVal Arg As String)
End Sub
Console.WriteLine("Proc( {0} )", Arg)
End Class
End Sub
End Class
Public Sub OnClick() ' event handler
Console.WriteLine("Button clicked")
Sub Main(ByVal CmdArgs() As String)
End Sub
Dim C As Tester = New Tester()
Dim D As MyDelegate = AddressOf C.Proc
Sub Main()
Dim Bt As Button = new Button()
D.Invoke(CmdArgs(0)) ' calls procedure
AddHandler Bt.ClickEvent, AddressOf OnClick
' pointed by delegate
Bt.PerformClick()
End Sub
39 End Sub 40

Summary of classes, structures


Event handlers and interfaces
Friend WithEvents Button1 As Windows.Forms.Button  Class
 defines a set of properties, methods and events
Protected Sub Button1_Click( _
ByVal sender As System.Object, _  reference type (allocated on the heap)
ByVal e As System.EventArgs) Handles Button1.Click  S tructure
 like class can contains data and methods
MsgBox("Button clicked")
End Sub  value type (stored on the stack)
 may not be inherited from
 Interface
 similar to class, but do not provide implementation

41 42

7
Questions?

You might also like