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 (VB.NET) and Example 9-4 (C#).
Example 9-3. 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 price As Double, _ ByVal title As String, _ ByVal ISBN as string) _Price = price _Title = title _ISBN = ISBN 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
Example 9-4. The Book class in C#
public class Book { private float price; private string title; private string isbn; 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;} } }
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")); bookList.Add(new Book(34.99f,"Teach ...
Get Programming ASP.NET, Second Edition 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.