Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers as arguments

Status
Not open for further replies.

catalina36

Programmer
Aug 28, 2000
36
US
Is it possible to pass a pointer/reference to a control as an argument to a function? Along the same lines, is it possible to pass a pointer to a control array to a function? If so, could you please provide a sample of the syntax for both the function call and the function header?

TIA
[sig][/sig]
 
If I understand you correctly, you have a variable in a routine and you want to call a function where that function will use the variable and change the variable??

If this is the case.....
You are probably using ByVal in your function header.
This basically creates a copy of whatever you pass in, so that any changes made to the argument will not be represented in the original.

If you use ByRef in your function header, you are passing a pointer to the variable / combobox / ... and then any changes made to the argument will be made to the original 'thing' as well.

Simon [sig]<p>Simon<br>[/sig]
 
Simon,

My question was really more directed at passing a reference to either a control or a control array. For instance, say I have a function that manipulates the contents of a textbox. What I'd like to be able to do is simply pass a reference to the relevant textbox as an argument to a function rather than passing the text contained within as an argument.

I hope this clarifies my question.

Thanks.
[sig][/sig]
 
You can do this by what I said before - maybe I did not explain it too well...

Function:
Public MyFunction(ByRef txtBox as TextBox) as String
txtBox.Text = &quot;Simon&quot;
End Function

Call the function:
MyString = MyFunction(txtMyTextBox(0))

This way, you can change the contents of the text box within the function, and pass a string back to the calling routine. If you don't want to pass anything back, you can change the function to a Sub.

Simon [sig]<p>Simon<br>[/sig]
 
Simon,

I get it now! It's Monday - I'm running a little slow.

Not to belabor the point, but can I pass a reference to an entire control array (of textboxes for instance) or am I limited to passing a pointer to a single element of a control array as per your example (i.e. MyFunction(txtMyTextBox(0))?

Thanks for your help!
Jon
[sig][/sig]
 
Before now, I did not know how to do this. I have just concocted this way, but I do not like it and there are probably better ways to do it.

Create a new project and add 6 text boxes in a control array to Form1 (Text1(0), Text1(1),...) and a command button, and paste the code below in the general decs of the form.

Option Explicit

Private Sub Command1_Click()

Dim MyString As String
Dim ControlArray() As TextBox
Dim i As Integer

For i = 0 To 5
ReDim Preserve ControlArray(i + 1)
Set ControlArray(i) = Form1.Text1(i)
ControlArray(i) = Text1(i)
Next i
MyString = MyFunction(ControlArray)

End Sub

Private Function MyFunction(ByRef txtArray() As TextBox) As String

Dim i As Integer

MsgBox UBound(txtArray) & &quot; elements&quot;
For i = LBound(txtArray) To UBound(txtArray) - 1
txtArray(i).Text = i
Next i

End Function


Hope this helps. [sig]<p>Simon<br>[/sig]
 
Simon,

Good stuff. Thanks for your help!

Jon [sig][/sig]
 
There is one other way to do this (that I know of, anyway):

Create a form with 6 textboxes on it, all named Text1, with the Index property set from 1 to 6 appropriately, and a command button.

Private Sub Command1_Click()

MyFunction Text1

End Sub

Private Function MyFunction(ByRef txtSource as Object)

Dim txtCurrent as TextBox

For Each txtCurrent in txtSource
'do whatever
Next txtCurrent

End Function

This is a more object-oriented approach, and utilizes the For...Each loop of a collection, so you don't have to worry about array bounds. If you want to early bind the txtSource object, I'm not sure what you would use. I used code very similar to this recently, and just used the Object keyword because I didn't have the time to research the exact object type, but I would suspect it is TextBox.

You may notice, if you have Intellisense activated in VB6 (where a property list pops up in the code window as you type), that if you type &quot;Text1.&quot; in the code window, some of the methods for a standard collection object will pop up, namely Count and Item (although there are also LBound and UBound methods, like an array). So, apparently a control array is actually a control collection.

Steve [sig][/sig]
 
Steve,

FYI – Passing a reference to a control array to an Object type parameter in MyFunction() worked as you said. You can also use a Variant type parameter. However, I was unable to use “less generic” types like Control and TextBox without generating a type mismatch error. Binding considerations aside, it seems like the Object type is the way to go.

Thanks for your help.

Jon
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top