p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning VB 6 (http://p2p.wrox.com/forumdisplay.php?f=75)
-   -   Referencing an object from within a procedure (http://p2p.wrox.com/showthread.php?t=8888)

mike123abc February 3rd, 2004 05:12 AM

Referencing an object from within a procedure
 
Hi,

I have several forms which each contain a similar Grid. I want to be able to process any such Grid with a single (public) Procedure, residing in the Main.bas module, instead of writing the same procedure again and again in each Form.

Question : is there a way of referencing an object belonging to, say, a Form, from within a procedure "implicitly" , AND NOT by calling it by its complete name ( Form99.Grid1.Row ) ?

The idea is that I would need an implicit variable/"pointer" to indicate the calling Form - similar, I think, to "Me". Then, I would be able to write something like

CallingForm.Flex1.Row = 2 etc, etc...

where "CallingForm" is the implicit "pointer" towards any of the forms which call the Procedure.

Thanks,

Mike





pgtips February 3rd, 2004 06:03 AM

No, you cannot reference a form implicitly from within a module, but why can't you pass in the form instance (Me) as an argument to the module function?

mike123abc February 3rd, 2004 07:25 AM

The Function that processes the Form (and all other forms that call it) is not in the Form's code, but in a separate code module, where all public functions of the Project reside.


pgtips February 3rd, 2004 08:36 AM

Yes I understand that, what I mean is why can't you change the module functions from:
Public Function DoSomethingWithGrid(arg1, ..., argN)
to
Public Function DoSomethingWithGrid(theForm, arg1, ..., argN)
then when you call DoSomethingWithGrid just pass in 'Me' as the first argument.

(You could instead pass in a reference to the actual Grid object rather than its containing form - I think I prefer that...)

hth
Phil

mike123abc February 3rd, 2004 09:08 AM

OK, I tried it and it works ("theForm" variable of DoSomething... defined as type "object").

You can then reference it (theForm.Flex.Row... ) from within the function and change the calling form or the objects it contains.

Thanks



marcostraf February 3rd, 2004 02:51 PM

It is a bad idea to assume that the global procedure knows the name of the grid in the form you are passing. As you wrote, it works only if the form has a grid named 'Flex'.
Much safer to pass the grid itself:

Public sub DoGrid(grd as MSFlexGrid, ...)

and call it from your form with:

DoGrid Me.NameOfTheGrid

In this way you can reuse you main code and any form can rename their grids as needed without breaking the code.

Marco


Quote:

quote:Originally posted by mike123abc
 OK, I tried it and it works ("theForm" variable of DoSomething... defined as type "object").

You can then reference it (theForm.Flex.Row... ) from within the function and change the calling form or the objects it contains.

Thanks





All times are GMT -4. The time now is 08:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.