Re: Declarations variables, Dim, some guidance please
- From: Neal Zimm <nealzimm@xxxxxxxxx>
- Date: Tue, 16 Aug 2005 13:49:06 -0700
Thanks to all for the sound advice, I hope y'all don't mind a common response.
I'm relatively new to the VB game, self taught (with help from this bulletin
board)
, and there's LOTS I don't know. (see later re: class variables.)
My uses of 'declared' vars in most cases, fall into all of the categories
mentioned.
1. vars where the values DON'T change, but are used by many subs. My
application is called "RM" and
I have a sub called zRM_Values which is called at the begining of larger
macros. (BTW, I have a bad
right pinky finger and typing quotes "" is a bother)
2 examples:
declared: dim Yes as string
in the zrm_values sub, Yes = "Y" , so in any other macro: if
other_var_name = yes then .....
declared: dim PressEnter as string
in zrm_values: PressEnter = "Press Enter or click OK after typing.." & vbcr
' used in inputbox dialogs
2. I have tried mightily to have the module declared vars fall into two
other categories:
a) they have the same meaning in any macro in the module so
if the value is changed, it's kinda a good thing for me that other macros
have access to the 'latest' value.
b) I don't care if the value changes.
example: dim x as integer
since I use option explicit, without dim'ing x in every procedure,
I can use: for x = 1 to whatever
3. I have not learned what a "class with properties" is yet, nor
what a collection class is. Is the Excel help adequate on this topic
or can you recommend other reading sources?
Again,
Thanks to all. Your help is very much appreciated.
Neal Z.
--
Neal Z
"JE McGimpsey" wrote:
> By preference (and it's *mostly* preference), I try to declare only
> procedure arguments and local (procedure-level) variables. This is
> especially useful in large applications where one may not be sure that a
> global/module-level variable isn't changed by an unrelated procedure.
>
> You didn't get a duplicate Dim error because one variable was declared
> at the module level, and one was declared within the sub. Local
> variables always override global variables within their own procedure.
> This is actually an advantage, as one can re-use variable names within
> multiple procedures, but each procedure sees only its own, local
> variable. For instance, note the reuse of "i" in:
>
> Public Sub Sub1()
> Dim i As Long
> For i = 1 To 10
> Sub2 "Iteration " & i & ": "
> Next i
> End Sub
>
> Public Sub Sub2(ByVal sStr As String)
> Dim i As Long
> For i = 1 To 3
> Debug.Print sStr & i
> Next i
> End Sub
>
> If your code is fairly small, and will never be maintained by anyone
> else, using module-level variables is probably fine, as long as you keep
> track of which procedures are modifying them, and when.
>
> If you're writing code that will be maintained by others, I'd strongly
> recommend using local variables as much as possible.
>
>
>
> In article <AEBDB225-BCB4-4A42-90DF-EF3175949C17@xxxxxxxxxxxxx>,
> Neal Zimm <nealzimm@xxxxxxxxx> wrote:
>
> > In an application that I'm developing I have dim'd quite a few variables in
> > Declarations. I'll admit some of it is not wanting to take the time to put
> > those vars that are used quite often in many macros within the sub
> > SubName(var list) parenthesis.
> >
> > 1) What advice can you offer on the pro's and cons of this technique? All of
> > the application's code is in ONE module.
> >
> > 2) I got 'bitten' when testing a macro where a var called Draw was dim'd as
> > integer in Declarations, had a good value > 0 in prior macros, but was 0 in
> > the macro I was testing.
> >
> > Sure enough, I had dim'd it again, inadvertantly, also as Integer in the
> > macro being tested. Hence the 0 value, I guess. The QUESTION is, why did I
> > NOT get a duplicate Dim error?
>
.
- References:
- Declarations variables, Dim, some guidance please
- From: Neal Zimm
- Re: Declarations variables, Dim, some guidance please
- From: JE McGimpsey
- Declarations variables, Dim, some guidance please
- Prev by Date: RE: Declarations variables, Dim, some guidance please
- Next by Date: Re: Help on VBA Page Breaks
- Previous by thread: Re: Declarations variables, Dim, some guidance please
- Next by thread: Re: Declarations variables, Dim, some guidance please
- Index(es):
Relevant Pages
|
Loading