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

Regarding Block level Scope & Variable Life Time

Hi all !

VB.NET adds the ability to create variables that are visible only within
a block. A block is any section of code that ends with one of the words
End , Loop , or Next . This
means that For...Next and If...End If blocks can have their own
variables.

So,

While y>6
Dim x as interger
......
End while

But x have lifetime of procedure level. Means,
Dim x as interger -- This line geting eff. at first time only. is it
right ?
Thanks & Regards

Elankathir,
B'lore,
India.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
10 3799
if you are asking how to use vars in loops i would say
(pseudo)
private sub bla
dim x as object
loop
x = new object
end loop
end sub

local in a for could also be

for i as integer = 0 to 100 step 1
array(i). bla = bla
next i

yust some thoughts

eric

"ElanKathir .S.N" <el*************@yahoo.co.in> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...
Hi all !

VB.NET adds the ability to create variables that are visible only within
a block. A block is any section of code that ends with one of the words
End , Loop , or Next . This
means that For...Next and If...End If blocks can have their own
variables.

So,

While y>6
Dim x as interger
......
End while

But x have lifetime of procedure level. Means,
Dim x as interger -- This line geting eff. at first time only. is it
right ?
Thanks & Regards

Elankathir,
B'lore,
India.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #2
Hi Elan,

Yes and they overrule the variables declared on a higher level outside the
method.

However you can not declare variables more times inside a method

It is in every block too every block from a try catch error block by
instance.

I hope this helps?

Cor
Nov 20 '05 #3
Elankathir and everybody else,

Here's a sample:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Short
For i = 1 To 10
Dim x As Short
x = 1
Next
Dim x As Short
MessageBox.Show("x = " & x)
End Sub

As shown, I get an error at the first declaration of x, saying that x hides
a variable declared in the enclosing block. This is interesting, since the
declaration is after the For loop. This appears to show that all
declarations happen before any code in a block is executed, regardless of
where the Dim statements are. Is that correct?

When I comment out the second declaration, I get an error in the message box
line saying that x is not accessible in this context because it is private.
It cannot be changed to protected or public.

I haven't tried it, but I'm pretty durn sure that in C++, a similar
construct would be perfectly acceptable. The inner x would be used in the
for loop, and the message box would show a random value because the outer x
would never have been initialized. There is no check for whether a variable
declaration in one scope hides a declaration in another scope. At most, a
warning, not an error, would have been generated.

I tried commenting out the inner declaration and leaving the outer
declaration. I got an error saying that x cannot be used before it is
declared.

Ah, well. This doesn't seem to be an issue I'm going to need to worry about
much in real life.

Rob
Nov 20 '05 #4
Seeing the message from Rob, I see I did not write it clear (I did want to
simplivy it)

I made a mistake in my message I see when I see your message, this is
allowed

for i as integer = 0 to 100
dim x =1
next
for i as integer = 0 to 100
dim x = 1
next

however this
dim x as integer
for i as integer = 0 to 100
dim x =1
next
for i as integer = 0 to 100
dim x = 1
next
will give an error

Cor


Nov 20 '05 #5
"Cor Ligthert" <no**********@planet.nl> schrieb
Yes and they overrule the variables declared on a higher level
outside the method.

However you can not declare variables more times inside a method

It is in every block too every block from a try catch error block
by instance.


Sorry Cor, but I really don't understand the last sentence. What do you
mean? Maybe the OP understands it but I'm really also interested.
--
Armin

Nov 20 '05 #6
Hi Armin,

I saw too this was a very quick written message.
It is in every block too every block from a try catch error block
by instance.

The same behaviour is for every kind of block, by instance too for a Try,
Catch, End Try block.

I hope this makes it even without the rest of the text more clear?

(I do not know why I write the last days try catch error block, now i see I
have to watch for that, I did that more times)

Thanks for making me attent on this

Cor
Nov 20 '05 #7
"Cor Ligthert" <no**********@planet.nl> schrieb
Hi Armin,

I saw too this was a very quick written message.


Now I understand. Your "too" was confusing me. :-)

"I saw (that) this was a very quickly written message, too."
"I also saw (that) this was a very quickly written message."

:-))

(I really hope that this is correct *g*)

It is in every block too every block from a try catch error
block by instance.

The same behaviour is for every kind of block, by instance too for a
Try, Catch, End Try block.

I hope this makes it even without the rest of the text more clear?

(I do not know why I write the last days try catch error block, now i
see I have to watch for that, I did that more times)


Sorry for correcting you, but I think this is allowed on Fridays at this
time (you (and everybody else) can correct me, too). ;-)))) The time should
be at the start or end of the sentence.

"I do not know why I write try catch error block (in) the last(past?) days,
now i see I have to watch for that, I did that many times"

Should only be a little hint because I'm often sitting in front of your
messages a long time. :-))) But don't care about me as long as everybody
else understands you. ;-)
--
Armin

Nov 20 '05 #8
> "I saw (that) this was a very quickly written message, too."
"I also saw (that) this was a very quickly written message."

I saw as well that this was a very quickly written message.

However let us not do to much correction in this sense, we loose it anytime
from a lot of others here.

:-))

Cor
Nov 20 '05 #9
"Cor Ligthert" <no**********@planet.nl> schrieb
"I saw (that) this was a very quickly written message, too."
"I also saw (that) this was a very quickly written message."

I saw as well that this was a very quickly written message.

However let us not do to much correction in this sense, we loose it
anytime from a lot of others here.

:-))


Sorry, I don't understand you. ;-))
--
Armin

Nov 20 '05 #10

Thanks to all!

Regards

Elankathir,
B'lore,
India.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #11

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

Similar topics

18
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
2
by: Anoj | last post by:
Hi All, As you all know in vb.net we can declare block level variables. Like : Dim I As Integer For I = 1 To 3 Dim N As Long ' N has block scope in VB.NET N = N + I Next
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
27
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
24
by: Neal Becker | last post by:
One thing I sometimes miss, which is common in some other languages (c++), is idea of block scope. It would be useful to have variables that did not outlive their block, primarily to avoid name...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
5
by: prashant.khade1623 | last post by:
HI I wrote a simple program to expect some error, but it is printing the value. main() { extern int i;
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.