Binding to a Class
In
the previous example, you bound an
ArrayList of strings to the list box. Often, you will want to bind
objects more complex than strings. For example, you might imagine a
Book class that has properties such as title,
ISBN, and price, as shown in Example 9-3 (C#) and
Example 9-4 (VB.NET).
Example 9-3. The Book class in C#
public class Book { public Book(float price, string title, string ISBN) { this.price = price; this.title = title; this.isbn = ISBN; } public float Price { get {return price;} } public string Title { get {return title;} } public string ISBN { get {return isbn;} } private float price; private string title; private string isbn; }
Example 9-4. The Book class in VB.NET
Public Class Book Private _Price As Double Private _Title As String Private _ISBN As String Public Sub New( _ ByVal thePrice As Double, _ ByVal theTitle As String, _ ByVal theISBN as string) _Price = thePrice _Title = theTitle _ISBN = theISBN End Sub Public ReadOnly Property Price( ) As Double Get Return _Price End Get End Property Public ReadOnly Property Title( ) As String Get Return _Title End Get End Property Public ReadOnly Property ISBN( ) As String Get Return _ISBN End Get End Property End Class
You add the new book objects to the ArrayList, just as you assigned the strings, shown here in C#. (The VB.NET code is the same except without the semicolons.)
bookList.Add(new Book(49.95f, "Programming ASP.NET","100000000")); bookList.Add(new Book(49.95f,"Programming C#","0596001177")); ...
Get Programming ASP .NET now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.