An Object Driven Interface with .Net
(Page 1 of 4 )
Using .Net, Paul will demonstrate how to generate an object driven interface. A rich source of support material is included with this article so you can start building interfaces today.
To make it possible for us to specify attributes related to our properties in our Business Object we will start by creating a custom attributes class; this class will help us to specify details we will later use in the Interface Generator.
The Custom Attributes Class
Our first step in creating a custom attributes class is to create a class that inherits from System.Attribute
<AttributeUsage(AttributeTargets.Property, AllowMultiple:=False)> _
Public Class WizardAttributes
Inherits System.Attribute
......
End Class
We specify in the Attribute usage attribute that the attribute can only be used on properties and that we do not allow multiple copies of this attribute per property, in order to get some meaningful data to our interface generator we will later use the WizardAttributes Class to add attributes to out property declarations some of the attributes we might want to specify for a property would be.
a. Type a System.Type Object that will allow us to specify the type of control we want to render for the property
b. ListBoxLines a Int16 That will allow us to specify how many lines a list box should display should we choose to use a listbox control for our property
c. Label, the label we want to display in our interface
d. Required, do we require a value from the user for this field
e. SectionName, we want to be able to divide the Property Page into sections similar to the way it is done in Visual Studio to do that we need to be able to specify a section name for Each property
f. Locked, to determine whether the property is rendered as read only
g. Helptext, to display to the user when he/she selects the rendered property
h. ListIndex for use with List Controls
i. SelectedIndex for use with listControls
Each of these fields will be created as public properties allowing you to use them when specifying attributes for a Property in the Business Object Class.
<AttributeUsage(AttributeTargets.Property, AllowMultiple:=False)> _
Public Class WizardAttributes
Inherits System.Attribute
'Private fields.
Private lType As System.Type
Private lListBoxLines As Int16
Private lLabel As String
Private lRequired As Boolean = False
Private lSectionName As String = "Misc"
Private lLocked As Boolean = False
Private lHelpText As String
Private lListIndex As Int16 = -1
Private lSelectedIndex As Int16 = -1
Private lValidationExpression As String = ""
Private lValidationText As String = ""
'This constructor defines the required label property.
Public Sub New(ByVal Label As String, ByVal Type As System.Type)
lLabel = Label
lType = Type
End Sub
Public Overridable Property Type() As System.Type
Get
Return lType
End Get
Set(ByVal Value As System.Type)
lType = Value
End Set
End Property
Public Overridable Property ListBoxLines() As Int16
......
End Class
Our attribute class has two required properties. These are Label and Type, In order for us to create an interface with any meaning, we need to have a Label to tell the user what we are asking and a Input Control of some sort such as a Textbox which is specified by the Type Property, we create a New Constructor with these two values are Parameters in the above code thereby making them required when the user adds the attributes to his property declaration.
The Base Class
Now that we have created our attributes class we have one more class that we need to create before we can start using attributes and create a business object. We can not pass a list of items to be used in for example a dropdown list as an attribute since attributes need to be constants, so in order for us to gain the ability to specify the elements in a dropdown list and to make sure our interface generator can always find these lists of items we are going to create a base class that our business object needs to inherit from, this base class will contain certain properties and functions that our interface generator will assume are always present in the objects we pass it.
Public MustInherit Class WizardBase
Private lLists As System.Collections.Hashtable
Private lList As System.Collections.Specialized.StringCollection
Public ReadOnly Property List(ByVal Index As Integer) As _ System.Collections.Specialized.StringCollection
Get
Return lLists(Index)
End Get
End Property
Public Function CreateList(ByVal ParamArray Items As String()) As Integer
lList = New System.Collections.Specialized.StringCollection
Dim item As String
For Each item In Items
lList.Add(item)
Next
If lLists Is Nothing Then
lLists = New System.Collections.Hashtable
End If
lLists.Add((lLists.Count + 1), lList)
Return lLists.Count
End Function
End Class
The class is marked as MustInherit which means it can not be created using the new keyword but must be inherited by another class, the class contains two collections lLists and lList, this allows us to add as many lists as we need to the class and as many items to each list as we need, the lists can be returned by calling the List Property with the index number of the list we want to use, this will the return a collection containing the items belonging to the list, it also has a Function CreateList that allows you to create the lists that you need.
That’s it for the Attributes and base class, now we can create a business object that will utilize what we have done so far.
Next: Part 2: Creating our Business Object >>
More ASP.NET Articles
More By Paul Stevens