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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a trick to passing a control as a function argument? 1

Status
Not open for further replies.

Jasen

IS-IT--Management
May 1, 2002
435
US
So, I have a function to move controls, such as:
Code:
private function MoveMe(byref obj as Frame) as boolean

   obj.Left = 0
etc, etc

I've tried multiple ways of passing in my frame name, but nothing works. I get type mismatch error. I figured just something like:
MoveMe(Frame1)
would work, and after failing that, tried frame1.name and frame1.Hwnd.
I've tried changing the argument to "Control" and "Object" in the function declaration, both give the same result.
I know this has to be possible, where's my mistake at?
 
Is the function in the same file as the form?

Try using Formname.Framename as the argument instead of just the frame name.

Lee
 
Use Control
Code:
MoveMe(byref obj as Frame) as boolean


David
 
Oops. That got changed back to what I copied. It should be this:

Code:
MoveMe(byref obj as Control) as boolean

sorry.

David
 
Do not call your function like this:

MoveMe (Frame1)

as it isn't doing what you think it is doing1. Instead use:

Call MoveMe(Frame1)

or

result=MoveMe(Frame1)




1WHat is happening is a combination of two things. Firstly, because you have not hinted otherwise (by use of Call or by ssigning the result of the function to a variable), VB evaluates the expression in the brackets first before passing it to the function. Secondly, VB objects have default properties. For a Frame control the default property is the caption - so when object Frame1 gets evaluated it actually returns a string containing the caption.

A search in this forum should find further discussion on the subject of parenthesis
 
Thanks, strongm, that's almost certainly what's happening with the OP. I always automatically used the Call way, so I never had this problem.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Gotcha. I always thought of the "call" as somewhat redundant and stopped using it.

I had made my function a boolean, because I had planned on eventually calling it in the form:

If Not MoveMe(frame1) then
'error handling
End If

I just hadn't implemented that code yet. Maybe that would have taken care of the problem as well?

Thanks for the replies.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top