RE: Declarations variables, Dim, some guidance please



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


"Jim Thomlinson" wrote:

> Good coding practice is to use global or module level variable declarations
> as little as possible. The big reason is that it makes the code very
> difficult to debug. At any given time it can be difficult to know the value
> of a variable because all procedures can access it. One small change to one
> procedure can have side effects on all of the procedures that access that
> variable. The problem is that when you make the change it is not at all
> obvious what all of the consequences are. If you are passing the variable
> then it is reasonalby obvious. The only reasons that I ever use Globals are:
>
> 1. I need a value determined at run time to persist. An example might be a
> password to log into some subsystems. I get the user to enter it once and
> that value is stored for as long as the program runs. Any time I need to log
> into the subsystem I just call up the value. I don't have to worry about who
> changed the value last becuase it is only set once at the beginning of
> exectuion.
>
> 2. I have a function or sub procedure that is called repeatedly (great for
> recursion). In order to speed up the exectution I will have it use global
> variables so that the variables do not need to be created and destroyed each
> time the procedure is called.
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Neal Zimm" 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?
> >
> > Thanks.
> > --
> > Neal Z
.



Relevant Pages

  • Re: Macro crashes if I run it twice
    ... > each sub that needs them....still get the error. ... the same variables, I will put all the macros in one module, and declare all ... > Dim xlBook As Excel.Workbook ... > Dim xlR As Excel.Range ...
    (microsoft.public.excel.programming)
  • Re: Saving custom toolbars with macros to work on other computers
    ... I added the msgbox as you suggested and all macros appear to be called ... Sub create_menubar ... Dim mac_names As Variant ... "Dave Peterson" wrote: ...
    (microsoft.public.excel.misc)
  • Re: Declarations variables, Dim, some guidance please
    ... My uses of 'declared' vars in most cases, fall into all of the categories ... declared: dim PressEnter as string ... it's kinda a good thing for me that other macros ... > placing in Declarations or a procedure. ...
    (microsoft.public.excel.programming)
  • Re: Declarations variables, Dim, some guidance please
    ... My uses of 'declared' vars in most cases, fall into all of the categories ... declared: dim PressEnter as string ... it's kinda a good thing for me that other macros ... >> variables in Declarations. ...
    (microsoft.public.excel.programming)
  • Re: Auto save???
    ... I've run a couple of simple macros. ... Sub Auto_Open ... Dim MacNames As Variant ... Dim OkToTryToSave As Boolean ...
    (microsoft.public.excel.misc)