1. Trang chủ
  2. » Công Nghệ Thông Tin

Professional ASP.NET 1.0 Special Edition- P29 potx

40 275 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 40
Dung lượng 772,31 KB

Nội dung

Implementing the Add Method The Add method allows a new Product to be added to the collection: Public Sub Add( Item as Product ) If Item Is Nothing Then throw new ArgumentException("Product cannot be null") End If _products.Add(Item.Code, Item) End Sub This method throws an ArgumentException if a Null item parameter is passed. If the parameter is not Null, the Code property of the passed Product is used as the key for the Product in the contained Hashtable. Depending on our requirements, we could perform additional business logic validation here and throw additional exceptions. Implementing the Remove Method The Remove method removes a Product from the collection. The implementation of the method simply calls the Remove method of the Hashtable: Public Sub Remove(Item as Product) _products.Remove(Item.Code) End Sub Implementing the Item Property The Item Property allows a Product to be retrieved from, or added to, the collection using the product code: Public Default Property Item(Code as String) as Product Get Item = CType(_products(Code), Product) End Get Set Add(Value) End Set End Property The implementation of the Set accessor calls the Add method in order to add the new product to the internal Hashtable. The process is implemented in away so that any business logic in the Add method (such as the Null check) is neither duplicated nor missed. Implementing the GetEnumerator Method So that our collection class can be used with the for each statements in Visual Basic.NET and C#, our collection class must have a method called GetEnumerator. Although not strictly necessary, we also implement the IEnumerable interface using the Visual Basic.NET Implements keyword. This is good practice and requires very little work: Public Class ProductCollection Implements IEnumerable ' ' Implement an enumerator for the products Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator GetEnumerator = _products.Values.GetEnumerator() End Function ' End Class The GetEnumerator method has to return an enumerator object that implements the IEnumerator interface. We could implement this interface by creating another class, but since our collection class is using a Hashtable internally, it makes much more sense for us to reuse the enumerator object provided by that class when its values are enumerated. The Hashtable.Values property returns an ICollection interface and since the ICollection interface derives from IEnumerable, we can call GetEnumerator to create an enumerator object for the collection of values. Using the Collection Class With the Product and ProductCollection classes created, we can use them just like the other collections we have seen in this chapter, but this time with no casting. For example: <% Dim products As ProductCollection Dim p As product products = New ProductCollection() p = New Product("CAR", "A New Car", 19999.99) products.Add(p) p = New Product("HOUSE", "A New House", 299999.99) products.Add(p) p = New Product("BOOK", "A New Book", 49.99) products(p.Code) = p Response.Write("<h4>Product List</h4>") For Each p In products Response.Write (p.Code & ", " & p.Description & ", " & p.Price) Response.Write("<br />") Next products.Remove( products("HOUSE") ) Response.Write("<h4>Product List (HOUSE removed)</h4>") For Each p In products Response.Write(p.Code & ", " & p.Description & ", " & p.Price) Response.Write("<br />") Next Response.Write("<p>") Response.Write("<br />Description for code CAR is: " & _ products("CAR").Description) %> The output of this code is shown here: Here is the complete ASP.NET code written in Visual Basic.NET for the Product and ProductCollection classes, as well as the ASP.NET page: <%@Page Language="VB"%> <script runat=server> ' Defines a product Public Class Product ' Private members dim _code as string dim _description as string dim _price as Double ' Constructor Sub New(initialCode as String, _ initialDescription as String, _ initialPrice as Double) Code = initialCode Description = initialDescription Price = initialPrice End Sub Public Property Description as String Get Description = _description End Get Set _description = Value End Set End Property Public Property Code as String Get Code = _code End Get Set _code = Value End Set End Property Public Property Price as Double Get Price = _price End Get Set _price = Value End Set End Property End Class ' Define a Products Collection Public Class ProductCollection Implements IEnumerable dim _products as Hashtable Public Sub New() _products = new Hashtable() End Sub ' Implement an enumerator for the products Public Function GetEnumerator() As IEnumerator _ Implements IEnumerable.GetEnumerator GetEnumerator = _products.Values.GetEnumerator() End Function Public Default Property Item(Code as String) as Product Get Item = CType(_products(Code), Product) End Get Set Add(Value) End Set End Property Public Sub Add(Item as Product) If Item is Nothing Then Throw New ArgumentException("Product can not be null") end if _products.Add(Item.Code,Item) End Sub Public Sub Remove( Item as Product ) _products.Remove( Item.Code ) End Sub End Class Protected Sub Page_Load( sender as object, events as EventArgs ) End Sub Dim _products As ProductCollection = new ProductCollection </script> <% Dim products As ProductCollection Dim p As product products = New ProductCollection() p = New Product("CAR", "A New Car", 19999.99) products.Add(p) p = New Product("HOUSE", "A New House", 299999.99) products.Add(p) p = New Product("BOOK", "A New Book", 49.99) products(p.Code) = p Response.Write("<h4>Product List</h4>") For Each p In products Response.Write (p.Code & ", " & p.Description & ", " & p.Price) Response.Write("<br />") Next products.Remove( products("HOUSE") ) Response.Write("<h4>Product List (HOUSE removed)</h4>") For Each p In products Response.Write(p.Code & ", " & p.Description & ", " & p.Price) Response.Write("<br />") Next Response.Write("<p>") Response.Write("<br />Description for code CAR is: " & _ [...]... Directory.SetCurrentDirectory("C:\Wrox") Response.Write("The current directory is " & _ Directory.GetCurrentDirectory() ) %> When we write an ASP.NET page we should make no assumptions about the current working directory Typically we should never need to change it, since we should not use relative filenames within ASP.NET pages Rather, we should use the Server.MapPath method to create a fully qualified filename from a relative... { Foo x = e.Current; // } } finally { ((IDisposable)e).Dispose(); } Summary In this chapter we've covered most of the interfaces and classes that comprise the System.Collections and System.Collection.Specialized namespaces We've examined the standard interfaces such as IEnumerable, IEnumerator, and ICollection, and we've discussed how these interfaces work together to provide a consistent way of creating... in object-oriented programming and is something familiar from classic ASP, when objects like ADO Connection or Recordset were used If I had to write a method to display the contents of a directory in ASP.NET, I'd probably design the method to accept a DirectoryInfo object rather than a string that represented the directory name It looks neater, feels right, and can have performance benefits if the... file classes, we'll look at some examples of how they can be used to perform common tasks, as well as some of the common exceptions that can be thrown Setting and Getting the Current Directory When an ASP.NET page is executed, the thread used to execute the code that generates the page will, by default, have a current working directory of %windir%\system32 If we pass a relative filename into any class... a Hashtable or ArrayList collection that can validate, and therefore restrict, the types it contains It's a simple process to create our own collection class by deriving from these classes This simple ASP.NET page defines a collection class called MyStringCollection, adds three strings and one integer, and then displays the contents: ' Our custom collection... manipulate paths The Path class allows us to: Extract the elements of a path, such as the root path, directory, filename, and extension Change the extension of a file or directory Combine paths Determine special characters, such as the path and volume separator characters Determine if a path is rooted or has an extension The Path class has the following methods: Method Name Parameters Description Path, . code is shown here: Here is the complete ASP. NET code written in Visual Basic .NET for the Product and ProductCollection classes, as well as the ASP. NET page: <%@Page Language="VB"%>. something familiar from classic ASP, when objects like ADO Connection or Recordset were used. If I had to write a method to display the contents of a directory in ASP. NET, I'd probably design. simple process to create our own collection class by deriving from these classes. This simple ASP. NET page defines a collection class called MyStringCollection, adds three strings and one integer,

Ngày đăng: 03/07/2014, 07:20