sloppy programmer: how to minimize module level scope varialbles

D

David

I've always been in a hurry to write programs and I always end up writing
alot of variables at the module level. Stuff that only needs to be in a
procedure I will only define in a procedure but if several procedures need
access to those variables that is when they end up at the top of my module.

I am fairly new to .net coming from VB6.

thanks for any suggestions.
 
M

Mr. Arnold

David said:
I've always been in a hurry to write programs and I always end up writing
alot of variables at the module level. Stuff that only needs to be in a
procedure I will only define in a procedure but if several procedures need
access to those variables that is when they end up at the top of my
module.

I am fairly new to .net coming from VB6.

You can make a public object/class that has accessor properties let/get for
the variables. That way, all classes can access the public object/class and
do let/get on the properties of the variables, as needed.
 
C

Cor Ligthert[MVP]

David,

One of the tricks is to use the properties from a global object.

By instance the text property op a textbox is when you use the designer
always there.

Be aware that using those cost mostly less then making a copy of it.

Cor
 
H

Herfried K. Wagner [MVP]

David said:
I've always been in a hurry to write programs and I always end up writing
alot of variables at the module level. Stuff that only needs to be in a
procedure I will only define in a procedure but if several procedures need
access to those variables that is when they end up at the top of my
module.

I assume you are talking about private variables such as in this scenario:

\\\
Privavate m_Value As Integer

Public Sub Foo()
m_Value = 12
Goo()
End Sub

Public Sub Goo()
MsgBox(m_Value)
End Sub
///

It's always a good idea to keep code units (classes, methods, blocks) as
modular as possible. This means that there should be as few as possible
external dependencies. The resulting code is easier to maintain and to
reuse.

What you can do in some cases is adding parameters to methods. In the above
example you could replace the implementation of 'Foo' with 'Goo(12)' and
extend 'Goo' by a 'Value' parameter ('Public Sub Goo(ByVal Value As
Integer)'. However, there are many other approaches depending on the exact
situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top