190 likes | 280 Views
Visual Basic .NET. Huimin Zhao. VB .NET and C# .NET. Sister languages Both are pure OO languages Virtually equivalent in capabilities Share the same .NET Framework Class Library Mainly differ in syntax. Payroll Example (VB). VB Syntax. Case insensitive ' for starting a comment
E N D
Visual Basic .NET Huimin Zhao
VB .NET and C# .NET • Sister languages • Both are pure OO languages • Virtually equivalent in capabilities • Share the same .NET Framework Class Library • Mainly differ in syntax
VB Syntax • Case insensitive • ' for starting a comment • No ; at end of statement • No { }. Use End for ending a block End Class, End Sub, End Function, End Property, End Get, End Set, End Try, End If, End Select, End Using, etc.
Imports (for using) a namespace Imports System • Boolean for bool Private isMaleValue As Boolean • Integer for int Dim i As Integer • Single character value "0"c If (c >= "0"c And c <= "9"c) Then
vbCrLf for newline character If (MessageBox.Show("Are you sure you want to update " + vbCrLf + CType(lstEmployees.SelectedItem, Employee).ToString(), • vbTab for tab character Return TitleName + vbTab _ + MaritalStatus + vbTab _ + Category + vbTab _ + HomeAddress.ToString()
Defining a local variable Dim employeeObject As Employee • Defining a class instance variable Private firstNameValue As String • Catch exception object Catch ex As Exception • Using a resource Using exampleObj As New ExampleObj exampleObj.SomeMethodEnd Using
CType for type casting CType(employeeObject, HourlyEmployee).Hours = Decimal.Parse(txtHoursWorked.Text) • = for equality, <> for inequality If (value = "") Then If (phoneNumberValue.Length <> 10) Then • And, Or, Not for &&, ||, ! If (c >= "0"c And c <= "9"c) Then If c < "A"c Or c > "Z"c Then IsMale = Not value
Is for comparing reference types • Nothing for null If (Not Input Is Nothing) Then • Typeof to check the type of an object If (TypeOf EmployeeObject Is SalariedEmployee) Then • () for array subscript Private Shared stateNamesValue As String() = {...} stateNamesValue(stateValue)
If – ElseIf – Else – End If If (value = "MALE" Or value = "M") Then IsMale = True ElseIf(value = "FEMALE" Or value = "F") Then IsMale = False Else Throw New Exception("...") End If
For loop Dim i As Integer For i = 0 To stateNamesValue.Length - 1 If (inputValue = stateAbbreviationsValue(i)) Then stateValue = i Return End If Next i
For Each loop Dim item As Employee For Each item In lstEmployees.Items Formatter.Serialize(output, item) Next item
Do While loopDim Employee As Employee Employee = CType(reader.Deserialize(Input), Employee) Do While (Not Employee Is Nothing) lstEmployees.Items.Add(Employee) Employee = CType(reader.Deserialize(Input), Employee) Loop
Function for method that returns value Private Function getInputs(employeeObject As Employee) As Boolean • Sub for a void method Private Sub displayEmployee( EmployeeObject As Employee)
Property for class property Public Property FirstName() As String • ReadOnly for read-only property Public ReadOnly Property Age() As Byte • Shared for static Public Shared ReadOnly Property States() As String()
Sub New for class constructor • Me for this Public Sub New(first As String,...) Me.FirstName = first
Inherits for inheritance Public Class HourlyEmployee Inherits Employee • myBasefor base Public Sub New() MyBase.New() End Sub
MustInherit for abstract class Public MustInherit Class Employee • Overridable for virtual method or property Public Overridable ReadOnly Property Category As String • MustOverride for abstract method or property Public MustOverride ReadOnly Property Earnings() As Decimal • Overrides for override Public Overrides ReadOnly Property Earnings() As Decimal