'Visual Basic 2008/2010 Properties Object Oriented Programming Namespace Example1 Class Person Private m_Name As String Public Property Name() As String Get ' (1) Name = m_Name End Get Set(ByVal value As String) ' (2) m_Name = value End Set End Property End Class Module Client Sub Main() Dim person1 As Person = New Person person1.Name = "John" ' (3) Console.WriteLine(person1.Name) ' (4) ' Console.ReadLine() End Sub End Module End Namespace Namespace Example2 Class Book Public Title As String End Class Module Client Sub Main() Dim book1 As Book = New Book book1.Title = "Some Title" Console.Write(book1.Title) ' Console.ReadLine() End Sub End Module End Namespace Namespace Example3 Class Counter Private m_Value As Integer Public Property Value() As Integer Get Value = m_Value End Get Private Set(ByVal value As Integer) ' (1) m_Value = value End Set End Property Public Sub Raise() Value = Value + 1 ' (2) End Sub End Class End Namespace Namespace Example3 Module Client Sub Main() Dim counter1 As Counter = New Counter Console.WriteLine(counter1.Value) ' counter1.Raise() Console.WriteLine(counter1.Value) ' counter1.Raise() Console.WriteLine(counter1.Value) ' 'counter1.Value = 5 ' impossible, no available setter (3) ' Console.ReadLine() End Sub End Module End Namespace Namespace Example4 Class Product Private m_Price As Decimal Public Property Price() As Decimal Get Price = m_Price End Get Set(ByVal value As Decimal) m_Price = value End Set End Property Private m_TaxPercentage As Decimal Public Property TaxPercentage() As Decimal Get TaxPercentage = m_TaxPercentage End Get Set(ByVal value As Decimal) m_TaxPercentage = value End Set End Property Public ReadOnly Property PriceIncludingTax() As Decimal Get PriceIncludingTax = Price * (1 + (TaxPercentage / 100)) End Get End Property End Class Module Client Sub Main() Dim product1 As Product = New Product product1.Price = 100 product1.TaxPercentage = 8 'product1.PriceIncludingTax = 108 ' impossible ( ReadOnly Property ) Console.WriteLine(product1.PriceIncludingTax) ' Console.ReadLine() End Sub End Module End Namespace Namespace Example5 Class Product Private m_Price As Decimal Public Property Price() As Decimal Get Price = m_Price End Get Set(ByVal value As Decimal) m_Price = value End Set End Property Private m_TaxPercentage As Decimal Public Property TaxPercentage() As Decimal Get TaxPercentage = m_TaxPercentage End Get Set(ByVal value As Decimal) m_TaxPercentage = value End Set End Property Public Function GetPriceIncludingTax() As Decimal GetPriceIncludingTax = Price * (1 + (TaxPercentage / 100)) End Function End Class Module Client Sub Main() Dim product1 As Product = New Product product1.Price = 100 product1.TaxPercentage = 8 'product1.GetPriceIncludingTax = 108 ' impossible ( Function ) Console.WriteLine(product1.GetPriceIncludingTax()) ' Console.ReadLine() End Sub End Module End Namespace Module BankAccountTestFixture Sub Main() Dim bankAccount1 As BankAccount = New BankAccount Console.WriteLine(bankAccount1.Balance = 0) ' bankAccount1.Deposit(100) Console.WriteLine(bankAccount1.Balance = 100) ' bankAccount1.Withdraw(60) Console.WriteLine(bankAccount1.Balance = 40) ' Dim bankAccount2 As BankAccount = New BankAccount bankAccount1.Transfer(50, bankAccount2) Console.WriteLine(bankAccount1.Balance = -10) Console.WriteLine(bankAccount2.Balance = 50) ' Console.ReadLine() End Sub End Module Class BankAccount Private m_Balance As Decimal Public ReadOnly Property Balance() As Decimal Get Balance = m_Balance End Get End Property Public Sub Deposit(ByVal amount As Decimal) m_Balance += amount End Sub Public Sub Withdraw(ByVal amount As Decimal) m_Balance -= amount End Sub Public Sub Transfer(ByVal amount As Decimal, ByVal target As BankAccount) Withdraw(amount) target.Deposit(amount) End Sub End Class 'Bezoek www.vbvoorbeelden.be voor meer Visual Basic voorbeelden. 'Copyright - De Wolf / vbvoorbeelden - 2003-2010 - Alle rechten voorbehouden.