Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of

Defining Named Arguments

Properties in your custom attribute become the named arguments. Because you apply attributes as a single constructor call, the properties need to be initialized in some way, and they are—as named arguments.

To wrap up, we'll limit the named arguments to a position for the generated control, the assembly-qualified name of a label (if we want one), a regular expression for validation, and whether the control is updatable. Our fictitious application doesn't use all of this information yet, but the sample code in the final section, "Testing the Attribute," demonstrates how to use much of it.

Listing 5 contains the completed custom attribute with all of the elements.

Listing 5—The Completed ControlDesignerAttribute

<AttributeUsage(AttributeTargets.Property, _
 Inherited:=True, AllowMultiple:=True)> _
Public Class ControlDesignerAttribute
 Inherits System.Attribute

 Private FControlName As String
 Private FCaption As String
 Private FX, FY, FWidth, FHeight As Integer
 Private FLabelName As String
 Private FValidationExpression As String
 Private FIsUpdatable As Boolean

 Public Sub New()
 End Sub

 Public Sub New(ByVal ControlName As String)
  FControlName = ControlName
 End Sub

 Public ReadOnly Property ControlName() As String
 Get
  Return FControlName
 End Get
 End Property

 Public Property Caption() As String
 Get
  Return FCaption
 End Get
 Set(ByVal Value As String)
  FCaption = Value
 End Set
 End Property

 Public Property X() As Integer
 Get
  Return FX
 End Get
 Set(ByVal Value As Integer)
  FX = Value
 End Set
 End Property

 Public Property Y() As Integer
 Get
  Return FY
 End Get
 Set(ByVal Value As Integer)
  FY = Value
 End Set
 End Property

 Public Property Height() As Integer
 Get
  Return FHeight
 End Get
 Set(ByVal Value As Integer)
  FHeight = Value
 End Set
 End Property

 Public Property Width() As Integer
 Get
  Return FWidth
 End Get
 Set(ByVal Value As Integer)
  FWidth = Value
 End Set
 End Property
 Public Property LabelName() As String
 Get
  Return FLabelName
 End Get
 Set(ByVal Value As String)
  FLabelName = Value
 End Set
 End Property

 Public Property ValidationExpression() As String
 Get
  Return FValidationExpression
 End Get
 Set(ByVal Value As String)
  FValidationExpression = Value
 End Set
 End Property

 Public Property IsUpdatable() As Boolean
 Get
  Return FIsUpdatable
 End Get
 Set(ByVal Value As Boolean)
  FIsUpdatable = Value
 End Set
 End Property

 Protected Sub SetBounds( _
  ByVal X As Integer, ByVal Y As Integer, _
  ByVal Width As Integer, ByVal Height As Integer)

  Me.X = X
  Me.Y = Y
  Me.Width = Width
  Me.Height = Height

 End Sub

End Class

The finished product contains fields and properties, rounding out the attribute class. Each of the properties can be passed as a named argument (except the read-only ControlName property) when you apply the attribute.

To use the custom attribute, we'll compile the assembly and reference it in another project.

  • + Share This
  • 🔖 Save To Your Account