472,378 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 1379
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
5
by: fred | last post by:
I don't know if I'm doing this correctly. I have a little programming experience in python, c++ and some others but this is my first time with javascript. I'm trying have my website detect the...
5
by: Ant | last post by:
hi, I'm now using C#. Seeing as though you can declare & initialize or pass a value to a variable on the same line as the declaration, is it still best practice to group all the variables together...
5
by: Fred Nelson | last post by:
Hi: I'm a relative newby so hopefully this is a simple question! I have found that I can create global variables easily on a web page by placing the dim statement before the first "private...
17
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
15
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here...
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
8
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: //...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.