469,356 Members | 2,596 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Declaring variables - best practice?

Hi all,

New to .Net - still finding my feet...quick question...

In one of my functions I have about a dozen variables being declared at the
top - the first thing within the function, about 2 of these may or may not
be used depending on a couple of IF...Then's further down the page...

What I'd like to know is - should I declare ALL of my variables at the top
like this whether they might be used or not - thus keeping them in one easy
to find location for debugging etc etc, or should I declare them just before
I need them, so in my example - maybe something like this :
Dim strString1 as String ' always used
Dim strString2 as String ' always used

If test = true Then

Dim strString3 as String

strString3 = results

End If
Any advice would be appreciated - I'd like to get this correct from the
outset if possible :o)

Thanks in advance for your replies,

Regards

Rob
Nov 18 '05 #1
2 1280
If the variables will ONLY be used INSIDE the if (caps for emphasis),
declare inside the if. No need to have them hanging outside of the scope, or
even instantiated. If you need them outside the if, you will have to declare
outside.

In general, I declare at the top, inside a section well labeled as
declarations. I make an exception for items scoped inside of a smaller
construct (for ... next, if, do, et al).

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Rob Meade" <ro**********@NOSPAMubht.swest.nhs.uk> wrote in message
news:u0**************@TK2MSFTNGP10.phx.gbl...
Hi all,

New to .Net - still finding my feet...quick question...

In one of my functions I have about a dozen variables being declared at the top - the first thing within the function, about 2 of these may or may not
be used depending on a couple of IF...Then's further down the page...

What I'd like to know is - should I declare ALL of my variables at the top
like this whether they might be used or not - thus keeping them in one easy to find location for debugging etc etc, or should I declare them just before I need them, so in my example - maybe something like this :
Dim strString1 as String ' always used
Dim strString2 as String ' always used

If test = true Then

Dim strString3 as String

strString3 = results

End If
Any advice would be appreciated - I'd like to get this correct from the
outset if possible :o)

Thanks in advance for your replies,

Regards

Rob

Nov 18 '05 #2
"Cowboy (Gregory A. Beamer)" wrote ...
If the variables will ONLY be used INSIDE the if (caps for emphasis),
declare inside the if. No need to have them hanging outside of the scope, or even instantiated. If you need them outside the if, you will have to declare outside.

In general, I declare at the top, inside a section well labeled as
declarations. I make an exception for items scoped inside of a smaller
construct (for ... next, if, do, et al).


Thanks Gregory, as I suspected but wanted to clarify :o)

Many thanks

Rob
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Oliver Corona | last post: by
5 posts views Thread by Ant | last post: by
5 posts views Thread by Fred Nelson | last post: by
10 posts views Thread by Jay Wolfe | last post: by
8 posts views Thread by rendle | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.