Re: Scope of Variant / Array



"J_Goddard via AccessMonster.com" <u37558@uwe> wrote in message
news:78906703ff51c@xxxxxx
Hi -

Even though you declared the array "public" in the main (parent) form, it
isn't really public - it is known only to the procedures within the parent
form's module, which means you cannot reference it in the sub-form's code.
As well, the parent. property cannot reference variables, only properties
of
the parent.

No, not true at all. note how KitCaz said it works just fine with variables.

Remember, a form code module is the same as a class module.

thus,

public m_setting as string


is a legal var def, and any routine can reference the above value by
supplying the forms reference, and then the var name
(assuming the form is open).

In fact, any public variable declared in a class module (including forms)
simply becomes a settable property of the form.

However, the following is NOT legal


public buf(10) as string

If kitCaz had actually tried to compile the above line of code, you see the
following error message:

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Constants, fixed-length strings, arrays, user-defined types and Declare
statements not allowed as Public members of object modules
---------------------------
OK Help
---------------------------


(I did a control-c while viewing the dialog box to copy the above).

The simply solution is to simply declare the array as private, and then
build pubic propriety for to expose the array
in the forms code.

The following will give programming access to the array.

Option Compare Database
Option Explicit

Private vbuf() As Variant

Public Property Let myVbuf(i As Integer, str As String)


vbuf(i) = str


End Property

Public Property Get myVbuf(i As Integer) As String

myVbuf = vbuf(i)

End Property


Now, in code, you can go:

me.parent.myVbuf(3) = "hello"

msgbox "3rd value should be hello...here it is " & myvbuf(3)

So, you *can* reverence any variable declared public in a form, you just
have to supply the forms qualifier..

me.MyVariblename
or

forms!NameOfForm.MyVariableName = "hello"

or
me.Parent.MyVariableName = "hello"

Since "me" is the default, then you also use:

MyVariablename

All are above are valid.. In fact, if you have form named zoo, then if you
reference the BASE class object name of that form, inteli-sense will
actually show the above public members (variables), and also the public
property we added as a drop down list.

In code, I often start typing the follwining

msgbox forms!Zoo.varname

(and, then I goo...hum, forgot the name..I don't want to go and look it
up..so, I type in:

forms_Zoo.

----------^ ..the instaant I hit the "dot", then inti-sense will popup a
list of varilbes and properites ad public functions. I can view, or even
select the varible name...I cut otu the whole line, and the continue typeing
my orignal line of code:

msgbox forms!Zoo.MyVar.......


To make a variable truly public, you must declare it as public in a
standard
code module.

Truly pubic, yes, but as a long as the form is open, that public var in that
form is useable by ANY code and any form, or any thing in the appcation, you
just have to provide a forms qualifier.

In the posters example, he does not need a global variable, but simply to
reference a variable that is part of parents form code. Further, if KitCaz
continues to code this way, then you can even have multiple instances of the
SAME form opened..and the variables will REMAIN separate from each other.
(just like a class object would.. I you use global here , they will trip
over each over, and you can't have multiple instances of the form function
correct if you use global -- they should be avoided here);

So, I *do* in fact recommend using public variables in a forms code module
for his case.

So, the ONLY limitation here is that arrays are NOT allowed to be public in
class object, and that also applies to a forms code module which is the same
as a class object...

However, as shower, simply adding a public property to the class modules
code, you can use nearly the same syntax, and you can expose the private
array by declaring a public property.


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@xxxxxxx


.



Relevant Pages

  • Re: Scope of Variant / Array
    ... a form code module is the same as a class module. ... Constants, fixed-length strings, arrays, user-defined types and Declare ... The simply solution is to simply declare the array as private, ...
    (microsoft.public.access.formscoding)
  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... In the header we would declare? ...
    (microsoft.public.vc.language)
  • Re: vb.net class
    ... about fixed array lenghts or using ReDim statements. ... code ensures everything in the array is a String because you declare it ... Count can be generated from the time list, not need to store ...
    (microsoft.public.dotnet.languages.vb)
  • Re: ReDim not working as expected. Array expert needed.
    ... Any dimensions you declare your variable in are wiped out, ... variable as a variant, I next told you not to redim it. ... examples of how array dimensions work in VBA. ...
    (microsoft.public.excel.programming)
  • Re: Looking to improve program
    ... You generally should declare your variables in the smallest scope ... array. ... Now that you have the hash, you can use it instead of four nearly ... There's also no real reason to be assigning ...
    (comp.lang.perl.misc)