locked
Moving a Sub from Derived Class to Base RRS feed

  • Question

  • User757861294 posted

    Hello all,

     Thank you for reading this.

    I have a set of custom controls that all inherit from a base class and implement an interface.

     

    Base Class

        Public MustInherit Class InputControlBase
            Inherits System.Web.UI.UserControl

     

    Interface

    Imports Microsoft.VisualBasic
    Namespace MyNamespace

        ''' <summary>
        ''' Indicates class is to be used as an input control
        ''' </summary>
        ''' <remarks></remarks>
        Public Interface IInputControl

        End Interface

    Derived Class (Custom Control)

     

     Partial Class MyInputControl
        Inherits MyInputControlBase
        Implements IInputControl

        Protected Overrides Sub PopulateMenu() Implements MyNamespace.IInputControl.PopulateMenu
            Dim oMenus As New myMenus
            oMenus.LoadSearchMenus(MyNamesapce.CategoryTradeType.Trades, Me.drpOptions)
            Me.drpOptions.Items.Insert(0, New ListItem(sDefaultSelectMessage, String.Empty))
        End Sub

    End Class

     

    Now I want to move the Sub PopulateMenu into my base class since the functionality is the same and needs isolating but I am having problems doing this.  How do I declare an object and code in my base class that can populate the control in my derived classes?  I tried creating a dropdownlist called drpOptions in the base class and returning it but when I created it in my derived classes I got problems informing me to declare it as Shadows but couldn't get it to work properly.  There were nullreferenceexception(s) being thrown in routines that attempt to access the dropdownlist item.

     I think the main problem is that my base class doesn't have a form attached (it resides in my App_Code folder) and isn't declared Partial.  I think I need someone to point me in the right direction for moving Page objects to a base class without causing null reference exceptions.

     Any help would be greatly appreciated.

    Thanks

     -CRandall

    Sunday, April 26, 2009 2:48 PM

Answers

  • User-364289655 posted

    What you can do is make your function take in the dropdownlist you're trying to populate by reference (byref), then you can use this on any page for any dropdown name. Something like:

      

    Protected Sub PopulateMenu(byref dd as DropDownList) Implements MyNamespace.IInputControl.PopulateMenu
         Dim oMenus As New myMenus
         oMenus.LoadSearchMenus(MyNamesapce.CategoryTradeType.Trades, dd)
         dd.Items.Insert(0, New ListItem(sDefaultSelectMessage, String.Empty))
    End Sub 

     

    Then you can call it from your pages PopulateMenu(drpOptions) and that'll do what you want.

    Hopefully that'll work for you

     ~P

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 26, 2009 3:14 PM

All replies

  • User-364289655 posted

    What you can do is make your function take in the dropdownlist you're trying to populate by reference (byref), then you can use this on any page for any dropdown name. Something like:

      

    Protected Sub PopulateMenu(byref dd as DropDownList) Implements MyNamespace.IInputControl.PopulateMenu
         Dim oMenus As New myMenus
         oMenus.LoadSearchMenus(MyNamesapce.CategoryTradeType.Trades, dd)
         dd.Items.Insert(0, New ListItem(sDefaultSelectMessage, String.Empty))
    End Sub 

     

    Then you can call it from your pages PopulateMenu(drpOptions) and that'll do what you want.

    Hopefully that'll work for you

     ~P

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 26, 2009 3:14 PM
  • User757861294 posted

     

    Ah! So simple but then the most beautiful solutions often are. Well done -marked as answer

     

     

    [:D] 

    Tuesday, April 28, 2009 3:18 PM