As mentioned earlier, you need to have a way to report multiple errors from the data-centric business logic to the user interface instead of one error at a time. The easiest way to do this is to pass an array of errors to the user interface. However, you need an easy way to add values to the array so that you are not calling ReDim Preserve all the time. To this end, you will create a new class called BusinessErrors . Before you do this, though, you need to create a new structure to hold your collection of errors. In the Structures code module in the NorthwindShared project, add the following structure:
<Serializable()> Public Structure structErrors Public errProperty As String Public errMessage As String End Structure
Next, add an Imports statement at the top of the Errors code module to import the Structures namespace:
Imports NorthwindTraders.NorthwindShared.Structures
Create the class shown in Listing 5-2 in the Errors code module in the Errors namespace.
Listing 5-2: The BusinessErrors Class
<Serializable()> Public Class BusinessErrors Inherits System.Collections.CollectionBase Public Sub Add(ByVal strProperty As String, ByVal strMessage As String) Dim obj As New structErrors() obj.errProperty = strProperty obj.errMessage = strMessage list.Add(obj) obj = Nothing End Sub Public Function Item(ByVal Index As Integer) As structErrors Return CType(list.Item(Index), structErrors) End Function Public Sub Remove(ByVal Index As Integer) list.RemoveAt(Index) End Sub End Class
In Listing 5-2, you are inheriting from the CollectionBase object because you will only need to loop through these items using a For..Next loop—you do not have to access anything by key. You will pass in the property that caused the error and the error message and add them to the list. You could require that a structError object be passed in, but that would require the user of this object to do a lot of extra work all over the place (the object would have to create a structure variable, assign the properties, and then pass the structure to the collection class). As it is, the work happens in only one place. Everything else is part of a standard collection class. Now it is time to start implementing your new class.