4 Replies - 2241 Views - Last Post: 18 October 2010 - 08:21 AM Rate Topic: -----

#1 VFP2VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-October 10

Inheritance in VB.Net

Posted 17 October 2010 - 03:03 AM

I have subclassed textboxes, panels and buttons. For each of those controls, I wrote an IOEnable method. I would like my (subclassed) form to execute these methods. I came up with something like this, which works, but am wondering if there is no more generic approach to this, because now, each time I subclass a control I have to kind of "register" it here and in every container.

           For Each ctrl As Control In Me.Controls
                If TypeOf ctrl Is txtBase Then
                    Dim ctrl1 As txtBase = ctrl
                    ctrl1.ioEnable(value)
                End If
                If TypeOf ctrl Is pnlBase Then
                    Dim ctrl1 As pnlBase = ctrl
                    ctrl1.ioEnable(value)
                End If
                If TypeOf ctrl Is btnBase Then
                    Dim ctrl1 As btnBase = ctrl
                    ctrl1.ioEnable(value)
                End If
            Next


In VFP this looked like this where "pemstatus(,,5)" returned true if the control "had" a method "IOEnable".

for each o in this.Controls

		o= ctr
		if !pemstatus(o,'IOEnable',5)
			loop
		endif
		o.IOEnable()
	endif
endfor


Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance in VB.Net

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,074
  • Joined: 02-July 08

Re: Inheritance in VB.Net

Posted 17 October 2010 - 06:26 AM

Not really sure about your question. What do you mean register it? Do you want more control over like adding it at design time so you can call it directly like your other controls? Could you elaborate?
Was This Post Helpful? 0
  • +
  • -

#3 VFP2VB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-October 10

Re: Inheritance in VB.Net

Posted 17 October 2010 - 07:17 AM

Well I might want to subclass other controls (cmbBase, lstBase, pgeBase) or code variation on txtBase (txtNumericBase, txtDateBase) etc. IF I want them to "participate" in that for next loop, I need to "register" them there. In vfp, I could check if the control "had" an "IOEnable".

This post has been edited by VFP2VB: 17 October 2010 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,074
  • Joined: 02-July 08

Re: Inheritance in VB.Net

Posted 17 October 2010 - 06:27 PM

I am very confused by the term 'register'. It may be a VFP thing. With Inheritance we are defining a base class where other class objects shall inherit the properties/methods/functions. Then as we create the classes that have this starting point we may define them futher, but they still have a defined base. In your case - one example the Button class is your base thought the Button inherits from ButtonBase and control,... object. Remember with Inheritance think of "Is A". Button 'is a' control and 'is a' object.

That is why this works for your button:

For Each ctr As Control In Me.Controls
  'your control's name will print
  Debug.WriteLine(ctr.Name)
  'but your methods will only show if you cast the type
  'CType(ctr, MyButton).ioEnabled(value)
Next


HTH
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,439
  • Joined: 29-May 08

Re: Inheritance in VB.Net

Posted 18 October 2010 - 08:21 AM

Have your controls implement a common interface.
For Example.
Public Interface IIOEnabled
  Sub ioEnable(ByVal State As Boolean)
End Interface


Which is then implemented
Public Class C
  Inherits TextBox
  Implements IIOEnabled

  Public Sub ioEnable(ByVal State As Boolean) Implements IIOEnabled.ioEnable

  End Sub
End Class


Then you can simply check for the interface.
Public Class Form1

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim value As Boolean = True

    Me.Controls.OfType(Of IIOEnabled).ToList.ForEach(Sub(x) x.ioEnable(value))

  End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1