Results 1 to 4 of 4

Thread: Using Objects in a Method

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    19

    Using Objects in a Method

    I have some procedures I want to store in a module and call up to use in the form.

    My problem is that the radio buttons are not being transferred into the module. I think I need a line of code at the beginning of the method so it has access to all the objects created in the form.

    The error I am getting is:
    [rb#] "is not declared. It may be inaccessible due to its protection level"

    rb# are radio buttons that are being used in the main form.

    form:
    vb Code:
    1. 'uncheck boxes
    2.         Call uncheckradiobuttons()

    method
    (rb# is underlined showing that is incorrect):
    vb Code:
    1. Public Module Module1
    2.  
    3.     Public Sub uncheckradiobuttons()
    4.         rb11.Checked = False
    5.         rb12.Checked = False
    6.         rb13.Checked = False
    7.         rb14.Checked = False
    8.         rb21.Checked = False
    9.         rb22.Checked = False
    10.         rb23.Checked = False
    11.         rb24.Checked = False
    12.         rb31.Checked = False
    13.         rb32.Checked = False
    14.         rb33.Checked = False
    15.         rb34.Checked = False
    16.         rb41.Checked = False
    17.         rb42.Checked = False
    18.         rb43.Checked = False
    19.         rb44.Checked = False
    20.         rb51.Checked = False
    21.         rb52.Checked = False
    22.         rb53.Checked = False
    23.         rb54.Checked = False
    24.         rb61.Checked = False
    25.         rb62.Checked = False
    26.         rb63.Checked = False
    27.         rb64.Checked = False
    28.     End Sub
    29.  
    30.  
    31. End Module

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Using Objects in a Method

    Yes, that's right. What you are missing is arguments to the method. Frankly, you almost certainly don't want to be doing this, as it violates the very spirit of object oriented programming, but the way to do it would look kind of like this:
    Code:
    Public Sub uncheckradiobuttons(frm As <your form type here>)        
     frm.rb11.Checked = False        
     frm.rb12.Checked = False     
    End Sub
    The problem is that such a method will only work if you supply it an instance of <your form type here>. Therefore, the method might as well be a method that is part of <your form type here>, rather than being over in a module. Of course, you could change that argument to be:

    frm As Form

    in which case it would take ANY form, but if the form you passed in didn't happen to have all those controls, the method would throw an exception. Therefore, there is no good reason to have that method anywhere other than as part of the form.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    19

    Re: Using Objects in a Method

    Thanks for your help. I guess even though it isn't advised and doesn't make a lot of since, I would still like to store some procedures in a method so the form is cleaner and easier to follow (this is for a homework assignment).

    I did this:
    vb Code:
    1. Public Module Module1
    2.  
    3.     Public Sub uncheckradiobuttons(frm As Form1)
    4.         frm.rb11.Checked = False
    5.         frm.rb12.Checked = False
    6.         ...
    7.     End Sub
    8. End Module

    However, now this line isn't working:
    vb Code:
    1. 'uncheck boxes
    2.         Call uncheckradiobuttons()
    It says:
    Argument not specified for parameter 'frm' of 'Public Sub uncheckradiobuttons(frm as Form1)'.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Using Objects in a Method

    Take a look at using #Regions to organize code in a file. You can also make use of partial classes to move parts of the code into other files without having it in a module.

    As for the problematic line, you aren't passing a form to it, and it requires one. Have you not covered arguments, yet?

    The proper call would be:

    uncheckradiobuttons(Me)

    as long as you are calling it from some instance of Form1 (which you probably are). Also, the Call word is never needed and does nothing, so you can always leave it out.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width