Home All Groups Group Topic Archive Search About

I declared the variable already, so why doesn't VB recognize it?

Author
13 May 2005 5:19 PM
Chris Lieb
I am restructuring a program and copied over some code for some properties
from a module to a form.  The code is in this style:

Private priVar as Boolean

Public Property Get pubVar() as Boolean
    pubVar = priVar
End Property

Public Property Let pubVar(nv as Boolean)
    priVar = nv

    'Do stuff to form
End Property

When I run the code, I get an error about the variable not being defined. 
It highlights the reference to priVar in the Let method and every other time
that I reference it in the Let method.  It does not give me this error in the
Get method, nor did I ever get this error when the code was in the module. 
Needless to say, it has me stumped.  Anyone have any idea what I did wrong?

--
Chris Lieb
UPS CACH, Hodgekins, IL
Tech Support Group - Systems/Apps

Author
13 May 2005 5:41 PM
Veign
Where are you trying to using the variable from?  If from within the same
form that the property is defined then the code should work.  If from within
a module or another form then you need to preface the variable with the form
name.

Like:
Form1.pubVar

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/


Show quote
"Chris Lieb" <ChrisL***@discussions.microsoft.com> wrote in message
news:757D0D8D-71FA-48CB-A699-B676B0AB0385@microsoft.com...
> I am restructuring a program and copied over some code for some properties
> from a module to a form.  The code is in this style:
>
> Private priVar as Boolean
>
> Public Property Get pubVar() as Boolean
>     pubVar = priVar
> End Property
>
> Public Property Let pubVar(nv as Boolean)
>     priVar = nv
>
>     'Do stuff to form
> End Property
>
> When I run the code, I get an error about the variable not being defined.
> It highlights the reference to priVar in the Let method and every other
time
> that I reference it in the Let method.  It does not give me this error in
the
> Get method, nor did I ever get this error when the code was in the module.
> Needless to say, it has me stumped.  Anyone have any idea what I did
wrong?
>
> --
> Chris Lieb
> UPS CACH, Hodgekins, IL
> Tech Support Group - Systems/Apps
Author
13 May 2005 5:50 PM
Peter Aitken
Show quote
"Chris Lieb" <ChrisL***@discussions.microsoft.com> wrote in message
news:757D0D8D-71FA-48CB-A699-B676B0AB0385@microsoft.com...
>I am restructuring a program and copied over some code for some properties
> from a module to a form.  The code is in this style:
>
> Private priVar as Boolean
>
> Public Property Get pubVar() as Boolean
>    pubVar = priVar
> End Property
>
> Public Property Let pubVar(nv as Boolean)
>    priVar = nv
>
>    'Do stuff to form
> End Property
>
> When I run the code, I get an error about the variable not being defined.
> It highlights the reference to priVar in the Let method and every other
> time
> that I reference it in the Let method.  It does not give me this error in
> the
> Get method, nor did I ever get this error when the code was in the module.
> Needless to say, it has me stumped.  Anyone have any idea what I did
> wrong?
>

I think that property procedures are reserved for classes.


--
Peter Aitken

Remove the crap from my email address before using.
Author
13 May 2005 5:57 PM
Veign
Forms are Classes - just a special type that has a graphical interface.

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/


Show quote
"Peter Aitken" <pait***@CRAPnc.rr.com> wrote in message
news:%231potP%23VFHA.3076@TK2MSFTNGP12.phx.gbl...
> "Chris Lieb" <ChrisL***@discussions.microsoft.com> wrote in message
> news:757D0D8D-71FA-48CB-A699-B676B0AB0385@microsoft.com...
> >I am restructuring a program and copied over some code for some
properties
> > from a module to a form.  The code is in this style:
> >
> > Private priVar as Boolean
> >
> > Public Property Get pubVar() as Boolean
> >    pubVar = priVar
> > End Property
> >
> > Public Property Let pubVar(nv as Boolean)
> >    priVar = nv
> >
> >    'Do stuff to form
> > End Property
> >
> > When I run the code, I get an error about the variable not being
defined.
> > It highlights the reference to priVar in the Let method and every other
> > time
> > that I reference it in the Let method.  It does not give me this error
in
> > the
> > Get method, nor did I ever get this error when the code was in the
module.
> > Needless to say, it has me stumped.  Anyone have any idea what I did
> > wrong?
> >
>
> I think that property procedures are reserved for classes.
>
>
> --
> Peter Aitken
>
> Remove the crap from my email address before using.
>
>
Author
13 May 2005 7:17 PM
Bob Butler
"Veign" <NOSPAMinveign@veign.com> wrote in message
news:up8jNX%23VFHA.584@TK2MSFTNGP15.phx.gbl
> Forms are Classes - just a special type that has a graphical
> interface.
> "Peter Aitken" <pait***@CRAPnc.rr.com> wrote in message
> news:%231potP%23VFHA.3076@TK2MSFTNGP12.phx.gbl...
>> I think that property procedures are reserved for classes.

And beyond that, property procedures also work just fine in standard
modules.  VB6 is a very flexible tool when you know how to use it.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
13 May 2005 7:20 PM
Veign
True - Just never think of putting property procedures in standard modules.
I guess if you wanted to encapsulate something that requires a hook /
subclass maybe if would make the module neater looking...

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/


Show quote
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:ONwNiB$VFHA.2424@TK2MSFTNGP10.phx.gbl...
> "Veign" <NOSPAMinveign@veign.com> wrote in message
> news:up8jNX%23VFHA.584@TK2MSFTNGP15.phx.gbl
> > Forms are Classes - just a special type that has a graphical
> > interface.
> > "Peter Aitken" <pait***@CRAPnc.rr.com> wrote in message
> > news:%231potP%23VFHA.3076@TK2MSFTNGP12.phx.gbl...
> >> I think that property procedures are reserved for classes.
>
> And beyond that, property procedures also work just fine in standard
> modules.  VB6 is a very flexible tool when you know how to use it.
>
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>
Author
13 May 2005 7:30 PM
Bob Butler
"Veign" <NOSPAMinveign@veign.com> wrote in message
news:e5p1hF$VFHA.228@TK2MSFTNGP12.phx.gbl
> True - Just never think of putting property procedures in standard
> modules. I guess if you wanted to encapsulate something that requires
> a hook / subclass maybe if would make the module neater looking...

I've used it when I want to initialize a "global" object on first use or
have other checks on changes made to some value.  Keeping the actual
variable private in the module and using a public property gives the best of
both worlds.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
13 May 2005 10:19 PM
J French
On Fri, 13 May 2005 12:30:23 -0700, "Bob Butler"
<tiredofit@nospam.com> wrote:

>"Veign" <NOSPAMinveign@veign.com> wrote in message
>news:e5p1hF$VFHA.228@TK2MSFTNGP12.phx.gbl
>> True - Just never think of putting property procedures in standard
>> modules. I guess if you wanted to encapsulate something that requires
>> a hook / subclass maybe if would make the module neater looking...
>
>I've used it when I want to initialize a "global" object on first use or
>have other checks on changes made to some value.  Keeping the actual
>variable private in the module and using a public property gives the best of
>both worlds.

It is also neat for writing things that work like :

     Mid$( S$, 2, 1 ) = "?"

Where one is not really 'Let'ting any Property
Author
13 May 2005 7:27 PM
Peter Aitken
Show quote
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:ONwNiB$VFHA.2424@TK2MSFTNGP10.phx.gbl...
> "Veign" <NOSPAMinveign@veign.com> wrote in message
> news:up8jNX%23VFHA.584@TK2MSFTNGP15.phx.gbl
>> Forms are Classes - just a special type that has a graphical
>> interface.
>> "Peter Aitken" <pait***@CRAPnc.rr.com> wrote in message
>> news:%231potP%23VFHA.3076@TK2MSFTNGP12.phx.gbl...
>>> I think that property procedures are reserved for classes.
>
> And beyond that, property procedures also work just fine in standard
> modules.  VB6 is a very flexible tool when you know how to use it.
>
> --

Yes, you are right - I tried it.


--
Peter Aitken

Remove the crap from my email address before using.
Author
13 May 2005 6:08 PM
Tom Esh
On Fri, 13 May 2005 13:50:10 -0400, "Peter Aitken"
<pait***@CRAPnc.rr.com> wrote:
>
>I think that property procedures are reserved for classes.

Not really. They work fine in forms too. (A Form is essentially a
class with a UI and some built-in methods.) FWIW prop proceedures will
also work in a bas module, though I'm not sure how useful that is.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Author
13 May 2005 7:01 PM
Chris Lieb
I figured it out after forcing VB to do a full compile.  It is because I was
declaring the variables after I had already declared some other properties. 
I hate how anal VB is about where things are placed and in what order they
appear.  C# would care less and allow me to declare each private variable
right above its public property declaration.  Oh well, one more peculiarity
to get used to, I guess.

Chris

Show quote
"Chris Lieb" wrote:

> I am restructuring a program and copied over some code for some properties
> from a module to a form.  The code is in this style:
>
> Private priVar as Boolean
>
> Public Property Get pubVar() as Boolean
>     pubVar = priVar
> End Property
>
> Public Property Let pubVar(nv as Boolean)
>     priVar = nv
>
>     'Do stuff to form
> End Property
>
> When I run the code, I get an error about the variable not being defined. 
> It highlights the reference to priVar in the Let method and every other time
> that I reference it in the Let method.  It does not give me this error in the
> Get method, nor did I ever get this error when the code was in the module. 
> Needless to say, it has me stumped.  Anyone have any idea what I did wrong?
>
> --
> Chris Lieb
> UPS CACH, Hodgekins, IL
> Tech Support Group - Systems/Apps
Author
13 May 2005 7:20 PM
Ken Halter
"Chris Lieb" <ChrisL***@discussions.microsoft.com> wrote in message
news:6104844E-63EB-4C02-85BA-61E6DD3A3013@microsoft.com...
>I figured it out after forcing VB to do a full compile.  It is because I
>was
> declaring the variables after I had already declared some other
> properties.

fwiw, I've removed the normal little, right-pointing blue arrow on the
toolbar and have replaced it with one that shows a slightly larger,
right-pointing blue arrow that always does a full compile... as well as
clearing the checkbox on "Tools/Options/General" that says "Compile on
Demand". Setting VB up this way will give you fewer "surprises".

> I hate how anal VB is about where things are placed and in what order they
> appear.  C# would care less and allow me to declare each private variable
> right above its public property declaration.  Oh well, one more
> peculiarity
> to get used to, I guess.
>
> Chris

That's funny. I don't find it anal at all to place all private variables in
the declarations section. If C# truly allows things like this then why's
everyone saying that VB generates spaghetti code? Sounds like keeping stuff
where it belongs is the first step away from spaghetti. fwiw, I've seen just
as much (if not more) junk C#/B# code as I have junk VB code. It's not the
tool, it's the author. VB Classic Rules the Win32/Desktop world.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..

AddThis Social Bookmark Button