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

Passing reference of a control to a function

Status
Not open for further replies.

demoman

Programmer
Oct 24, 2001
242
US
Howdy,

I want to pass a control as a parameter to a sub. For example:

Private Sub ShowTotals (ctlTextBox as TextBox) as single
ctlTextBox.Text = 125 * 10
End Sub

Private Sub ShowStuff()
ShowTotals (frmMain.txtTotals)
End sub

As written, this produces a type mismatch when 'ShowTotals' is called. I tried a vanilla "as Control", but had the same results.

Appreciate any help as always.

-Steve-


 
Try this:

Code:
[b]Function[/b] ShowTotals(ctlTextBox As TextBox) As Single
  ctlTextBox.Text = 125 * 10
End Function


Private Sub ShowStuff()
  ShowTotals frmMain.txtTotals
End Sub

 
Thanks - for some reason I thought I was suppose to get smarter with age and experience. Perhaps, I have passed that threshold?
 
I'd suggest a keyword search in this forum for a discussion of brackets, but it is too wide a keyword to get anything meaningful

In summary, without any given cause to do otherwise, VB will evaluate the expression contaoned withing brackets before passing it on to anything else.

So:

ShowTotals (frmMain.txtTotals)

actually evaluates frmMain.txtTotals, which happens to be a string because a Textbox's default property is .Text. So you end up passing a string to the Sub - which is of course a type mismatch

bclt's solution works not because the sub has been redefined as a function, but becaus the brackets have been removed from the call, thus eliminating the expression evaluation

 
strongm,

Thanks for the additonal clarification. I did recognize the brackets (in bctl's post)as the issue, not the fact that is was defined as a function. When I first started by original post, I had the example as a function, but then changed my mind and made it a sub. I simply forgot to delete the returned value.

For countless years, every function I worked with required function arguments to be enclosed in parentheses. It took awhile to get it into my head when to use them, and when not to. But, sometimes I still slip into my old habits.

I think I am going to have to do as you suggest, research some past threads on bracketing. Probably do to my limited experience, it does not make sense to me why VB would try to evaluate the text box's default property. I guess I will find out. I am much more focused now on doing things right than just making them work. I still have quite a ways to go in using good OO techniques.
 
strongm,

Private Sub ShowTotals (ctlTextBox as TextBox) as single
...

This in bold is wrong. Only a function can return a type e.g. Single. If i want to call something that does many things, i'll remove the "As Single" and make a public|private Sub.
 
Sure, but that was clearly just a cut and paste error, since you can't actually get the IDE to accept a Sub declaration with a return parameter

 
Just FYI: if you look up "brackets" you will never find what you were looking for. Brackets are the square things you put around TGML tags here. What you want are "parentheses". That still wouldn't have helped much, however. You probably want to look for "function call" or something similar.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top