simple OO question

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

hi all,

a simple OO related question...

I have some controls. Say, a textbox, a combo and a datepicker.
They all share some properties (tag, size etc).

How should I add another shared property to them all? Eg a property
called "RequiredField" so I can go textbox.requiredfield=true,
combo.requiredfield=false etc etc

I guess I will be using some kind of inheritance. If I was doing just
one type of control it would be easy. I'm after an elegant and proper
way of adding to all types in one go.

Hope this makes some kind of sense!
 
You mention the Tag property, I thought this type of use is exactly
what it is for. It is an Object which can be any datatype you need,
boolean in this case.
 
Tim said:
I have some controls. Say, a textbox, a combo and a datepicker.
They all share some properties (tag, size etc).
How should I add another shared property to them all?
I guess I will be using some kind of inheritance. If I was doing just
one type of control it would be easy. I'm after an elegant and proper
way of adding to all types in one go.

Since you're deriving from each Control Type, you can't use Inheritance
to add "new" properties, so you're left with Interfaces.

Create an Interface that defines your extra properties, etc., then
implement them in each derived Control as a [public] property, as in

Public Interface ICommonProperties
Function RequiredField() As Boolean
End Interface

Public Class CustomTextBox
Inherits TextBox
Implements ICommonProperties

Public Function RequiredField() As Boolean _
Implements ICommonProperties.RequiredField
...
End Function
...
End Class

Often, you'd have a /private/ function implementing the /public/
interface, but doing it this way, you can still use the RequiredField
property directly against your control, as in

Friend WithEvents txtCustom1 As CustomTextBox
....

Me.txtCustom1.RequiredField = True

If you leave the property private, you'd have to cast the control to the
Interface type before you could set it (which you can do anyway), as in

For Each eCtl As Control in Me.Controls ' I know...
If TypeOf eCtl Is ICommonProperties Then
DirectCast(eCtl, ICommonPropeties).RequiredField = False
End If
Next

HTH,
Phill W.
 
Search the docs for Extender properties. This is what the ToolTip
provider control does.
 
Tim,

I absote don't see what you want to do but Tag is from the Type Ojbect.

You can add any object to it, as well one that containts thousand properties
again and even in that again objects.

Cor
 
I have some controls. Say, a textbox, a combo and a datepicker.
They all share some properties (tag, size etc).

How should I add another shared property to them all? Eg a property
called "RequiredField" so I can go textbox.requiredfield=true,
combo.requiredfield=false etc etc

Do what Cor suggested. Create a class with everything of interest to you,
namely RequiredField, etc. Set the Tag field of the controls to a new
instance of this class, and all the controls are extended.
 
Back
Top