473,385 Members | 1,409 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,385 software developers and data experts.

Position of declaration statements

Hi

I've been coding in VB for a good number of years and have recently made the
switch to VB.NET. When coding I put all my declaration/Dim statements at
the beginning of the Sub or Function before any "real" code. VB.NET lets
you scatter declaration statements throughout a Sub or Function and it seems
even have conditional declaration statements, ie;

If x = "something" then
dim y as Integer
dim z as Integer

<some code>

End if

Are there any performance advantages/disadvantages to be gained/lost by only
declaring variables immediately before you're going to use them as shown in
the above example? I've looked through the MSDN and .NET documentation for
any performance tips like this. Can someone point me to a good resource
that discusses these types of issues?

Thanks.

Peter
Nov 20 '05 #1
7 2036
"Peter Merwood" <peter_m@*antispam*circle-consulting.co.nz> schrieb
I've been coding in VB for a good number of years and have recently
made the switch to VB.NET. When coding I put all my declaration/Dim
statements at the beginning of the Sub or Function before any "real"
code. VB.NET lets you scatter declaration statements throughout a
Sub or Function and it seems even have conditional declaration
statements, ie;

If x = "something" then
dim y as Integer
dim z as Integer

<some code>

End if

Are there any performance advantages/disadvantages to be gained/lost
by only declaring variables immediately before you're going to use
them as shown in the above example? I've looked through the MSDN and
.NET documentation for any performance tips like this. Can someone
point me to a good resource that discusses these types of issues?

Variables at block scope are just like those declared at the start of the
procedure. The only difference is that they can be accessed only in the same
block, and consequently there can be variables with the same name in the
same procedure but in different blocks.

Whenever you declare something like this

for i= 1 to 100
dim y as New TheClass
next

the New statement is executed 100 times.
--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #2
Armin

OK - so what you're saying is that it doesn't really matter where your
declaration statements are positioned as long as you're aware of the scope
rules and also take account of what you're initialising, (ie; your "New"
example).

Have I got that right?

Peter,
"Armin Zingler" <az*******@freenet.de> wrote in message
news:OF**************@TK2MSFTNGP11.phx.gbl...
"Peter Merwood" <peter_m@*antispam*circle-consulting.co.nz> schrieb
I've been coding in VB for a good number of years and have recently
made the switch to VB.NET. When coding I put all my declaration/Dim
statements at the beginning of the Sub or Function before any "real"
code. VB.NET lets you scatter declaration statements throughout a
Sub or Function and it seems even have conditional declaration
statements, ie;

If x = "something" then
dim y as Integer
dim z as Integer

<some code>

End if

Are there any performance advantages/disadvantages to be gained/lost
by only declaring variables immediately before you're going to use
them as shown in the above example? I've looked through the MSDN and
.NET documentation for any performance tips like this. Can someone
point me to a good resource that discusses these types of issues?

Variables at block scope are just like those declared at the start of the
procedure. The only difference is that they can be accessed only in the

same block, and consequently there can be variables with the same name in the
same procedure but in different blocks.

Whenever you declare something like this

for i= 1 to 100
dim y as New TheClass
next

the New statement is executed 100 times.
--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #3
* "Peter Merwood" <peter_m@*antispam*circle-consulting.co.nz> scripsit:
you scatter declaration statements throughout a Sub or Function and it seems
even have conditional declaration statements, ie;

If x = "something" then
dim y as Integer
dim z as Integer

<some code>

End if

Are there any performance advantages/disadvantages to be gained/lost by only
declaring variables immediately before you're going to use them as shown in
the above example? I've looked through the MSDN and .NET documentation for
any performance tips like this. Can someone point me to a good resource
that discusses these types of issues?


Variables declared in a block will be created when the procedure is
entered. You can see that when you use "ildasm.exe". Nevertheless,
these variables cannot be used outside the block they are declared in.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #4
"Peter Merwood" <peter_m@*antispam*circle-consulting.co.nz> schrieb
OK - so what you're saying is that it doesn't really matter where
your declaration statements are positioned as long as you're aware of
the scope rules and also take account of what you're initialising,
(ie; your "New" example).

Have I got that right?


Yes, I think it can be expressed this way. :-) But I'd wait for statments
from other people,too. ;-) Personally I prefer the smallest scope possible.
--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #5
* "Armin Zingler" <az*******@freenet.de> scripsit:
OK - so what you're saying is that it doesn't really matter where
your declaration statements are positioned as long as you're aware of
the scope rules and also take account of what you're initialising,
(ie; your "New" example).

Have I got that right?


Yes, I think it can be expressed this way. :-) But I'd wait for statments
from other people,too. ;-) Personally I prefer the smallest scope possible.


I prefer the smallest scope possible too because it makes maintainance
of the code easier.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #6
Herfried

OK that clears it up. It seems it doesn't matter where you're declaring
your local variables (within a Sub or Function) as they're going to be
created when the Sub or Function is entered.

Thanks for clarifying this.

Peter,

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bn*************@ID-208219.news.uni-berlin.de...
* "Peter Merwood" <peter_m@*antispam*circle-consulting.co.nz> scripsit:
you scatter declaration statements throughout a Sub or Function and it seems even have conditional declaration statements, ie;

If x = "something" then
dim y as Integer
dim z as Integer

<some code>

End if

Are there any performance advantages/disadvantages to be gained/lost by only declaring variables immediately before you're going to use them as shown in the above example? I've looked through the MSDN and .NET documentation for any performance tips like this. Can someone point me to a good resource
that discusses these types of issues?


Variables declared in a block will be created when the procedure is
entered. You can see that when you use "ildasm.exe". Nevertheless,
these variables cannot be used outside the block they are declared in.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>

Nov 20 '05 #7
* "Peter Merwood" <peter_m@*antispam*circle-consulting.co.nz> scripsit:
OK that clears it up. It seems it doesn't matter where you're declaring
your local variables (within a Sub or Function) as they're going to be
created when the Sub or Function is entered.


It does matter in matters of readability and maintainability of the
source code.

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
2
by: Henrik S. Hansen | last post by:
I'm new to C++, and cannot figure out why this won't compile: std::map<std::string, int> tst; tst = 1; int main() { /*...*/ } It gives me: error: ISO C++ forbids declaration of `tst' with...
9
by: Michael Mair | last post by:
Hello, in C89 (at least in the last public draft), "3.6.2 Compound statement, or block", we have ,--- | Syntax | | compound-statement: | { ...
6
by: alex | last post by:
I'm playing around with css popups a la http://www.meyerweb.com/eric/css/edge/popups/demo.html for here: http://www.redbrick.dcu.ie/~alex/golf/courseinfo.html (css)...
22
by: James Brodie | last post by:
I just wanted to get some advice on what are the better practices: 1) Should variables be declared at the beginning of a function or just before they are required? 2) Should all variabled be...
21
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value...
18
by: sunny | last post by:
Hi Why does C allows declaration of variable inside switch block. ex: foll prg does not gives "undeclared "b" error msg. but also does not initialize b to 20 int a=1; switch(a) { int b=20;...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.