dcsimg
Results 1 to 9 of 9

Thread: Can you pass object names when overloading a function?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2015
    Posts
    132

    Resolved Can you pass object names when overloading a function?

    I'd like to make a function where I can cause a checkbox being checked to disable a control on a form by simply going:

    disablebox(Textbox1, Checkbox1)

    The code would be loaded into the form change event.

    Would this work? If not, could it be modified to work?

    Code:
        Public Function disablebox(ByVal dimMe As Object, ByVal dimCheckbox As Object)
    
    
            If dimCheckbox.checked = True Then
    
                dimMe.enabled = False
    
            Else
    
                dimMe.enabled = True
            End If
    
        End Function
    Last edited by ks101; Feb 2nd, 2016 at 09:57 AM.

  2. #2
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can you pass object names when overloading a function?

    If won't work if you have Option Strict set to On (which you should have). But if disMe could be any control then you could use that code if you use this signature instead:
    Code:
    Public Function disablebox(ByVal dimMe As Control, ByVal dimCheckbox As CheckBox)
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Can you pass object names when overloading a function?

    Yes. Slowly and in a somewhat ugly manner, you could write a method that, either by Reflection or looping recursively through the Form's Controls property, could take a Control's Name property as an argument then act on that control.

    But most people find it much easier to make an array or Dictionary and store the controls in there. Say you have three or four checkboxes and three or four text boxes. You could add this in and around your Load handler:
    Code:
    Private _checkLookup As New Dictionary(Of String, Checkbox)
    Private _textboxLookup As New Dictionary(Of String, TextBox)
    
    Private Sub Form1_Load(...)
        _checkLookup.Add(Checkbox1.Name, Checkbox1)
        _checkLookup.Add(Checkbox2.Name, Checkbox2)
        ...
    
        _textboxLookup.Add(TextBox1.Name, TextBox1)
        _textboxLookup.Add(TextBox2.Name, TextBox2)
        ...
    End Sub
    If you have a lot of check/text boxes, there's ways to loop through the form's Controls property, but it's very hard to write a version that works on every form. There's a few other ways to reduce this tedium, but they involve making your controls in code instead of on the designer and are also hard to showcase. Anyway.

    Now that you have that, your function can take a name:
    Code:
    Public Sub DisableBox(ByVal textBoxName As String, ByVal checkBoxName As String)
        Dim theTextBox = _textboxLookup(textBoxName)
        Dim theCheckBox = _checkLookup(checkBoxName)
        theTextBox.Enabled = Not theCheckBox.Checked
    End Sub
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,029

    Re: Can you pass object names when overloading a function?

    The generic Object data type doesn't have any properties etc, but the more specific Control data type does, including .Enabled ; simply changing the data type for that (and specifying the appropriate data type for the Checkbox) should work.

    Note also that it should be a Sub rather than a Function, as it is not designed to return a value.


    edit: it took long than I thought to make a coffee!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2015
    Posts
    132

    Re: Can you pass object names when overloading a function?

    Quote Originally Posted by si_the_geek View Post
    The generic Object data type doesn't have any properties etc, but the more specific Control data type does, including .Enabled ; simply changing the data type for that (and specifying the appropriate data type for the Checkbox) should work.

    Note also that it should be a Sub rather than a Function, as it is not designed to return a value.


    edit: it took long than I thought to make a coffee!
    Would that still be inefficient per the guy above you, or is that the best solution for what I am trying to accomplish?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,929

    Re: Can you pass object names when overloading a function?

    Yeah, that would be pretty efficient, but it doesn't really matter either way. Both solutions would happen so fast that you wouldn't see them occur. If you had lots of other stuff going on, then perhaps it might matter, but it really seems unlikely that you'd be able to detect it.
    My usual boring signature: Nothing

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Can you pass object names when overloading a function?

    Quote Originally Posted by ks101 View Post
    Would that still be inefficient per the guy above you, or is that the best solution for what I am trying to accomplish?
    He's commenting more on the fact that, with the settings that professional developers prefer for VB, your code will not compile. And even with the more lax default settings, it's dangerous, because there's nothing stopping you from passing a String instead of a CheckBox, then throwing an exception at runtime because String doesn't have a Checked property.

    Long story short, they're saying, "the code example won't work at all as-is", but I think you were asking more, "This is pseudocode that looks like what I want, how do I write actual code that does this?" I think you already knew that code wouldn't work.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,029

    Re: Can you pass object names when overloading a function?

    I think it will work using the example usage syntax shown in post #1 (which uses actual controls, rather than names as the thread title implies), but should have the data-type changes I mentioned (for correctness, and for help in the code editor).

  9. #9
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Can you pass object names when overloading a function?

    There seems to be two different 'questions' here:

    Passing an object of a certain type to a function to disable that object;
    passing a name of an object to a function to disable that named object.

    The subject line requires a name (i.e. a string) to be passed to a method. Sitten's code addresses this issue using a dictionary (dictionaries are very fast, even in the grand scheme of things).

    Joacim's post addresses the former, passing a specific control reference to the method. Si, I think, is suggesting passing a generic Control, which the CheckBox and TextBox inherit from, to the method.

    Any of these methods will work.

    However, it appears there are CheckBox/TextBox pairs that are associated with one another. I think the Dictionary method may be the best in this case. Indeed, instead of passing the Checkbox and TextBox names separately, have just a 'pairName' and access each using that PairName.

    Code:
    Public Sub DisableBox(ByVal pairName As String)
        Dim theTextBox = _textboxLookup(pairName)
        Dim theCheckBox = _checkLookup(pairName)
        theTextBox.Enabled = Not theCheckBox.Checked
    End Sub
    This does require a 1 to 1 relationship, so may not be appropriate. It does open up a possibility of creating your own object which encapsulates a checkBox and a textBox (or a collection of textBoxes?) and have a single dictionary of those objects. It isn't complicated, but may be beyond the basics at this point.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width