How do I pass a null value to a function in VB6?

What I need: show the user one message. However, sometimes I need to show in the textbox and at other times in the Combobox.

What I did: I created two functions that get: First function 1. The command I want to show to the user 2. The name of the text field that will contain the message

function txtfunction(Byval msg as String, Byval txt as Textbox)

      

Second function 1. The command I want to show to the user 2. The name of the Combobox that will contain the message

function cbxfunction(Byval msg as String, Byval cbx as ComboBox)

      

It works, but I would like to know if it is possible to mix these two functions in only one. For when I call the function, I can set one parameter (textbox or Combobox) to zero.

The function I want:

function txtcbxfunction(Byval msg as String, Byval txt as TextBox, Byval cbx as ComboBox)

      

What I expect to be called:

txtcbxfunction("HELLO",nameTextBox, null) 'Message in the TextBox
txtcbxfunction("HELLO",null,nameComboBox) 'Message in the ComboBox

      

+3


source to share


1 answer


TextBox

and ComboBox

are part of the VB class Control

, so you can represent them in general with an object Control

.

For example:

Function txtcbxfunction(ByVal msg As String, c As Control)
    c.Text = msg  ' Both TextBox and ComboBox have a "Text" property, so this works
End Function

      

And then call it the same way, whether you use TextBox

or ComboBox

:



txtcbxfunction "Test Message", Text1
txtcbxfunction "Test Message", ComboBox1

      

If you want only the functions TextBox

and ComboBox

have been transferred to your function, you can use the function TypeName()

to determine the type of control:

Function txtcbxfunction(ByVal msg As String, c As Control)
    Select Case TypeName(c)
        Case "TextBox", "ComboBox"
            c.Text = msg
        Case Else
            Exit Function
    End Select
End Function

      

+4


source







All Articles