Вы находитесь на странице: 1из 4

Class Exercise

Class Employee

Dim salary As Decimal = 40000


Dim yearlyBonus As Decimal = 4000

Public Sub PrintSalary()


' print the salary to the Console
System.Console.Write(salary)
End Sub

End Class

Module Module1
Public Sub Main()

Dim anEmployee As Employee


anEmployee = New Employee()
anEmployee.PrintSalary()

End Sub
End Module

Imports System

Class Employee

Public salary As Decimal = 40000


Public yearlyBonus As Decimal = 4000

Public Sub PrintSalary()


' print the salary to the Console
Console.Write(salary)
End Sub

End Class

Class Manager: Inherits Employee


End Class

Module Module1
Public Sub Main()
Dim manager As Manager
manager = New Manager()
manager.PrintSalary()
End Sub
End Module
Creating Methods
Imports System

Module Exercise

Class TRectangle
Public Length As Double
Public Height As Double

Function Perimeter() As Double


Return (Length + Height) * 2
End Function
Function Area#()
Return Length * Height
End Function
End Class

Sub Main()
Dim Recto As New TRectangle
Dim Perimeter As Double
Dim Area As Double

Console.Write("Enter Rectangle Length: ")


Recto.Length = Console.ReadLine()
Console.Write("Enter Rectangle Height: ")
Recto.Height = Console.ReadLine

Console.WriteLine()
Console.WriteLine("Rectangle Characteristics")
Console.WriteLine("Length: {0}", Recto.Length)
Console.WriteLine("Height: {0}", Recto.Height)
Console.WriteLine("Perimeter: {0}", Recto.Perimeter)
Console.WriteLine("Area: {0}", Recto.Area)

Console.WriteLine()
End Sub

End Module

Add two methods of the class:


Imports System

Module Exercise

Public Class DepartmentStore


Public ItemNumber As Long
Public ItemName As String
Public UnitPrice As Double

Public Sub RegisterStoreItem()


Console.Write("Enter Item #: ")
ItemNumber = CLng(Console.ReadLine())
Console.Write("Enter Item Name: ")
ItemName = Console.ReadLine()
Console.Write("Enter Unit Price: $")
UnitPrice = CDbl(Console.ReadLine())
End Sub

Public Sub ShowItem()


Console.WriteLine("Item #: {0}", ItemNumber)
Console.WriteLine("Item Name: {0}", ItemName)
Console.WriteLine("Unit Price: {0}", UnitPrice)
End Sub
End Class

Sub Main()
Dim dptStore As DepartmentStore = New DepartmentStore

Console.WriteLine(" =#= Department Store =#=")


Console.WriteLine(" --- Item Registration ---")
Console.WriteLine("Enter the following pieces of information")
dptStore.RegisterStoreItem()

Console.WriteLine()
Console.WriteLine(" =#= Department Store =#=")
Console.WriteLine(" --- Store Inventory ---")
dptStore.ShowItem()
End Sub

End Module
 
Create a method that takes arguments 
Imports System

Module Exercise

Class DepartmentStore
Public ItemNumber As Long
Public ItemName As String
Public UnitPrice As Double

Public Sub RegisterStoreItem(ByVal nbr As Long, _


ByVal name As String, ByVal price As Double)
ItemNumber = nbr
ItemName = name
UnitPrice = price
End Sub

Public Sub ShowItem()


Console.WriteLine("Item #: {0}", ItemNumber)
Console.WriteLine("Item Name: {0}", ItemName)
Console.WriteLine("Unit Price: {0}", UnitPrice)
End Sub
End Class

Sub Main()
Dim dptStore As DepartmentStore = New DepartmentStore

Dim itmNbr As Long


Dim itmName As String
Dim uPrice As Double

Console.Write("Enter Item #: ")


itmNbr = CLng(Console.ReadLine())
Console.Write("Enter Item Name: ")
itmName = Console.ReadLine()
Console.Write("Enter Unit Price: $")
uPrice = CDbl(Console.ReadLine())

dptStore.RegisterStoreItem(itmNbr, itmName, uPrice)


dptStore.ShowItem()
End Sub

End Module

Writing a text files

Dim oFile as System.IO.File


Dim oWrite as System.IO.StreamWriter
oWrite = oFile.CreateText(“C:\sample.txt”)

oWrite.WriteLine(“Write a line to the file”)


oWrite.WriteLine()  

oWrite.Close()

Reading text files

Dim intSingleChar as Integer

 Dim cSingleChar as String

 oRead = oFile.OpenText(-C:\sample.txt")

 While oRead.Peek <> -1

 intSingleChar = oRead.Read()

 ' Convert the integer value into a character

 cSingleChar = Chr(intSingleChar)

 End While

Вам также может понравиться