473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1443
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.