VB.Net/Language Basics/Structure — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:42, 26 мая 2010
Содержание
Compare Structure and Class
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim cperson1 As New ClassPerson
Dim cperson2 As ClassPerson
cperson1.FirstName = "A"
cperson2 = cperson1
cperson2.FirstName = "B"
Console.WriteLine(cperson1.FirstName & ", " & cperson2.FirstName)
Dim sperson1 As New StructurePerson
Dim sperson2 As StructurePerson
sperson1.FirstName = "A"
sperson2 = sperson1
sperson2.FirstName = "B"
Console.WriteLine(sperson1.FirstName & ", " & sperson2.FirstName)
End Sub
End Class
Public Class ClassPerson
Public FirstName As String
Public LastName As String
End Class
Public Structure StructurePerson
Public FirstName As String
Public LastName As String
End Structure
Pass Structure into a Function
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
" create an instance of the structure
Dim loc1 As New Location(200, 300)
" display the values in the structure
Console.WriteLine("Loc1 location: {0}", loc1)
" invoke the default constructor
Dim loc2 As New Location( )
Console.WriteLine("Loc2 location: {0}", loc2)
" pass the structure to a method
myFunc(loc1)
" redisplay the values in the structure
Console.WriteLine("Loc1 location: {0}", loc1)
End Sub
" method takes a structure as a parameter
Shared Public Sub myFunc(ByVal loc As Location)
" modify the values through the properties
loc.XVal = 50
loc.YVal = 100
Console.WriteLine("Loc1 location: {0}", loc)
End Sub "myFunc
End Class
Public Structure Location
" the Structure has private data
Private myXVal As Integer
Private myYVal As Integer
" constructor
Public Sub New( _
ByVal xCoordinate As Integer, ByVal yCoordinate As Integer)
myXVal = xCoordinate
myYVal = yCoordinate
End Sub "New
" property
Public Property XVal( ) As Integer
Get
Return myXVal
End Get
Set(ByVal Value As Integer)
myXVal = Value
End Set
End Property
Public Property YVal( ) As Integer
Get
Return myYVal
End Get
Set(ByVal Value As Integer)
myYVal = Value
End Set
End Property
" Display the structure as a String
Public Overrides Function ToString( ) As String
Return [String].Format("{0}, {1}", xVal, yVal)
End Function "ToString
End Structure "Location
Simple Structure Demo
Imports System
Public Class MainClass
Shared Sub Main()
"Declare a Customer object
Dim objNewCustomer As Customer
"Create the new customer
objNewCustomer.FirstName = "f"
objNewCustomer.LastName = "l"
objNewCustomer.Email = "e"
System.Console.WriteLine(objNewCustomer)
End Sub
End Class
Public Structure Customer
"Public members
Public FirstName As String
Public LastName As String
Public Email As String
"Name property
Public ReadOnly Property Name() As String
Get
Return FirstName & " " & LastName
End Get
End Property
""" <summary>
""" Overrides the default ToString method
""" </summary>
""" <returns>String</returns>
""" <remarks>Returns the customer name and email address</remarks>
Public Overrides Function ToString() As String
Return Name & " (" & Email & ")"
End Function
End Structure
Store Structure into a Collection
Imports System
Imports System.Data
Imports System.Collections
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim customers As CustomerCollection = New CustomerCollection()
customers.Clear()
Dim newCustomer As Customer
newCustomer.FirstName = "firstName"
newCustomer.LastName = "lastName"
newCustomer.Email = "email"
" add it to the list...
customers.Add(newCustomer)
End Sub
End Class
Public Structure Customer
Public FirstName As String
Public LastName As String
Public Email As String
Public ReadOnly Property Name() As String
Get
Return FirstName & " " & LastName
End Get
End Property
Public Overrides Function ToString() As String
Return Name & " (" & Email & ")"
End Function
Public Function IsEmpty() As Boolean
If FirstName = "" Then
Return True
Else
Return False
End If
End Function
End Structure
Public Class CustomerCollection
Inherits System.Collections.CollectionBase
Private _emailHashtable As New Hashtable()
Public Sub Add(ByVal newCustomer As Customer)
Me.List.Add(newCustomer)
Dim useEmail As String
useEmail = newCustomer.Email.ToLower
EmailHashtable.Add(useEmail, newCustomer)
End Sub
Public Sub Remove(ByVal removeCustomer As Customer)
Me.List.Remove(removeCustomer)
Dim useEmail As String
useEmail = removeCustomer.Email.ToLower()
EmailHashtable.Remove(useEmail)
End Sub
Default Public Property Item(ByVal index As Integer) As Customer
Get
Return Me.List.Item(index)
End Get
Set(ByVal Value As Customer)
Me.List.Item(index) = Value
End Set
End Property
Public ReadOnly Property EmailHashtable() As Hashtable
Get
Return _emailHashtable
End Get
End Property
Default Public ReadOnly Property Item(ByVal email As String) _
As Customer
Get
email = email.ToLower()
Return EmailHashtable.Item(email)
End Get
End Property
Public Shadows Sub Clear()
MyBase.Clear()
EmailHashtable.Clear()
End Sub
Public Shadows Sub RemoveAt(ByVal index As Integer)
Remove(Item(index))
End Sub
End Class
Structure overrides ToString method
Imports System
Public Class MainClass
Shared Sub Main()
Dim loc1 As Location
loc1.XVal = 75
loc1.YVal = 225
" display the values in the structure
Console.WriteLine("Loc1 location: {0}", loc1)
End Sub
End Class
Public Structure Location
Private myXVal As Integer
Private myYVal As Integer
Public Sub New( _
ByVal xCoordinate As Integer, ByVal yCoordinate As Integer)
myXVal = xCoordinate
myYVal = yCoordinate
End Sub "New
Public Property XVal( ) As Integer
Get
Return myXVal
End Get
Set(ByVal Value As Integer)
myXVal = Value
End Set
End Property
Public Property YVal( ) As Integer
Get
Return myYVal
End Get
Set(ByVal Value As Integer)
myYVal = Value
End Set
End Property
Public Overrides Function ToString( ) As String
Return String.Format("{0}, {1}", XVal, YVal)
End Function
End Structure
Structure Variable assignment
Imports System
Imports System.Data
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim S1 As New StringStructure("Hello")
Dim S2 As StringStructure
S2 = S1
S2.SetString("Modified")
Console.WriteLine("Struct: " + S1.AString)
End Sub
End Class
Public Structure StringStructure
Public AString As String
Public Sub SetString(ByVal newstring As String)
AString = newstring
End Sub
Public Sub New(ByVal InitialString As String)
AString = InitialString
End Sub
End Structure
Structure with Constructor
Imports System
Public Class MainClass
Shared Sub Main()
Dim loc1 As New Location(200, 300)
Console.WriteLine("Loc1 location: {0}", loc1)
Dim loc2 As New Location( )
Console.WriteLine("Loc2 location: {0}", loc2)
myFunc(loc1)
Console.WriteLine("Loc1 location: {0}", loc1)
End Sub
Shared Public Sub myFunc(ByVal loc As Location)
loc.XVal = 50
loc.YVal = 100
Console.WriteLine("Loc1 location: {0}", loc)
End Sub
End Class
" declare a Structure named Location
Public Structure Location
" the structure has private data
Private myXVal As Integer
Private myYVal As Integer
" constructor
Public Sub New( _
ByVal xCoordinate As Integer, ByVal yCoordinate As Integer)
myXVal = xCoordinate
myYVal = yCoordinate
End Sub "New
" property
Public Property XVal( ) As Integer
Get
Return myXVal
End Get
Set(ByVal Value As Integer)
myXVal = Value
End Set
End Property
Public Property YVal( ) As Integer
Get
Return myYVal
End Get
Set(ByVal Value As Integer)
myYVal = Value
End Set
End Property
" Display the structure as a String
Public Overrides Function ToString( ) As String
Return String.Format("{0}, {1}", xVal, yVal)
End Function "ToString
End Structure "Location
ToString Method for Structure data type
Imports System
Imports System.Data
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim a As StringStructure
a.SetString("Hello")
Dim b As StringStructure = a
Console.WriteLine("B"s String: " + b.AString)
Dim c As New StringStructure("Another string")
Console.WriteLine("C"s String: " + c.AString)
Console.WriteLine("C"s ToString: " + c.ToString())
Dim obj As Object
obj = c
Console.WriteLine(obj.ToString())
End Sub
End Class
Public Structure StringStructure
Public AString As String
Public Sub SetString(ByVal newstring As String)
AString = newstring
End Sub
Public Sub New(ByVal InitialString As String)
AString = InitialString
End Sub
End Structure