Regarding Block level Scope & Variable Life Time

E

ElanKathir .S.N

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.
 
E

EricJ

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
 
C

Cor Ligthert

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
 
A

Armin Zingler

Cor Ligthert said:
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.
 
R

Rob Richardson

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
 
C

Cor Ligthert

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
 
C

Cor Ligthert

Hi Armin,

I saw too this was a very quick written message.
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
 
A

Armin Zingler

Cor Ligthert said:
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*)

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. ;-)
 
C

Cor Ligthert

"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
 
A

Armin Zingler

Cor Ligthert said:
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. ;-))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top