simple OO question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim

    #1

    simple OO question

    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 "RequiredFi eld" so I can go textbox.require dfield=true,
    combo.requiredf ield=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!

  • zacks@construction-imaging.com

    #2
    Re: simple OO question

    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.

    Comment

    • Tim

      #3
      Re: simple OO question

      thanks, but I need several. I just gave one example.

      Comment

      • Phill W.

        #4
        Re: simple OO question

        Tim wrote:
        [color=blue]
        > 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?[/color]
        [color=blue]
        > 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.[/color]

        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 ICommonProperti es
        Function RequiredField() As Boolean
        End Interface

        Public Class CustomTextBox
        Inherits TextBox
        Implements ICommonProperti es

        Public Function RequiredField() As Boolean _
        Implements ICommonProperti es.RequiredFiel d
        ...
        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.R equiredField = 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 ICommonProperti es Then
        DirectCast(eCtl , ICommonPropetie s).RequiredFiel d = False
        End If
        Next

        HTH,
        Phill W.

        Comment

        • Chris Dunaway

          #5
          Re: simple OO question

          Search the docs for Extender properties. This is what the ToolTip
          provider control does.

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: simple OO question

            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


            "Tim" <Citizen10Bears @gmail.com> schreef in bericht
            news:1146055236 .881526.316220@ e56g2000cwe.goo glegroups.com.. .[color=blue]
            > thanks, but I need several. I just gave one example.
            >[/color]


            Comment

            • Tim

              #7
              Re: simple OO question

              thanks all.
              some interesting stuff there.

              Comment

              • AMercer

                #8
                RE: simple OO question

                > I have some controls. Say, a textbox, a combo and a datepicker.[color=blue]
                > They all share some properties (tag, size etc).
                >
                > How should I add another shared property to them all? Eg a property
                > called "RequiredFi eld" so I can go textbox.require dfield=true,
                > combo.requiredf ield=false etc etc[/color]

                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.

                Comment

                Working...