 |
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

February 3rd, 2004, 05:12 AM
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

February 3rd, 2004, 06:03 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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?
|

February 3rd, 2004, 07:25 AM
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

February 3rd, 2004, 08:36 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|

February 3rd, 2004, 09:08 AM
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

February 3rd, 2004, 02:51 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
|
 |