Home All Groups Group Topic Archive Search About
Author
26 Apr 2006 11:18 AM
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!

Author
26 Apr 2006 12:28 PM
zacks
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.
Author
26 Apr 2006 12:40 PM
Tim
thanks, but I need several. I just gave one example.
Author
26 Apr 2006 1:38 PM
Cor Ligthert [MVP]
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


Show quoteHide quote
"Tim" <Citizen10Be***@gmail.com> schreef in bericht
news:1146055236.881526.316220@e56g2000cwe.googlegroups.com...
> thanks, but I need several. I just gave one example.
>
Author
26 Apr 2006 12:38 PM
Phill W.
Tim wrote:

> 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.
Author
26 Apr 2006 1:13 PM
Chris Dunaway
Search the docs for Extender properties.  This is what the ToolTip
provider control does.
Author
26 Apr 2006 2:29 PM
Tim
thanks all.
some interesting stuff there.
Author
26 Apr 2006 2:40 PM
AMercer
> 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.