How to access a control's methods from another module RRS feed

  • Question

    I have a project with many forms and each form has a ComboBox in it. I wrote a method which populates these comboBoxes. Since this method is common for all the forms i've placed it in a seperate module and then call it with the form as parameter. This used to work fine with VB6 but VB.Net is unable to access the control's properties on the form. Here is what i did:

    'Form1  'Form1 has a ComboBox named cmbBox

    Private Sub Form1_Load(....)

        FillComboBox (Me, True)        'FillComboBox method is defined in a module called Standard.vb

    End Sub


    Public Function FillComboBox(ByRef f As Form, ByRef LoadAll As Boolean)
        f.cmbMasterID.Items.Add("foo")    'ERROR: 'cmbMasterID' is not a member of 'System.Windows.Forms.Form'
    End Function

    What am i doing wrong ? how can i access the combo box's methods from the module?


    Wednesday, June 18, 2008 5:58 AM


  • Hi,

    Your approach breaks incapsulation, and therefore is not acceptable from Object Oriented Programming prospective.

    But anyway, if you still want be able to make it this way, you have to options:
    1. make necessary methods public
    2. Access private methods using Reflection.

    Vitaliy Liptchinsky
    • Marked as answer by Bruno Yu Monday, June 23, 2008 5:50 AM
    Wednesday, June 18, 2008 11:05 AM