Defining variables in a block?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to define a variable in a block in order to make
it invisible outside that block? For example, in C I can write

{
int a
....
}

then a will only be available inside the curley brackets

In Pascal there is 'Begin/End'

tnx
 
LucioH said:
Is it possible to define a variable in a block in order to make
it invisible outside that block? For example, in C I can write

{
int a
...
}

then a will only be available inside the curley brackets

In Pascal there is 'Begin/End'

Nothing *exactly* like { } or Begin/End I can think of; however you can
use any of the existing block constructs to simulate this sort of
purely syntactic block:

Do
....
Loop Until True

If True Then
....
End If

With New Object
....
End With
 
(replying to self)

Larry said:
Nothing *exactly* like { } or Begin/End I can think of; however you can
use any of the existing block constructs to simulate this sort of
purely syntactic block:

Do
...
Loop Until True

If True Then
...
End If

With New Object
...
End With

Better than the above is this (which I didn't think would work, but
does), which avoids creating an object:

With Nothing
....
End With
 
Lucio,

A in a block dimensioned value/object is always invisible outside a block

I hope this helps,

Cor
 
Yes but please think about WHY you would need to do this, or anything like
this. If the idea is code obfuscation, then it's all good ;)


Larry Lard said:
(replying to self)

Larry said:
Nothing *exactly* like { } or Begin/End I can think of; however you can
use any of the existing block constructs to simulate this sort of
purely syntactic block:

Do
...
Loop Until True

If True Then
...
End If

With New Object
...
End With

Better than the above is this (which I didn't think would work, but
does), which avoids creating an object:

With Nothing
...
End With
 
are you talking aboun of..

Private Enum myData As Integer
Name = 0
LastName = 1
End Enum

Private Structure myStruct
Dim Name As String
Dim LastName As String
End Structure

the type enum are for constants declaration... and the structure are for
datatype...
 
Is it possible to define a variable in a block in order to make
it invisible outside that block? For example, in C I can write

{
int a
...
}

In any *arbitrary* block of code, no.

In any syntactic construct or statement (If, Do, For, Select, etc.),
then yes.

Dim i1 as Integer = 0

For i1 = 1 To 300
Dim i2 as Integer = 17
' i2 only available within this loop
Next

HTH,
Phill W.
 
Lucio,
I would question the *real* need for this, as it sounds like (smells like)
the method is too long & attempting to do too much; the "Long Method" smell
in Refactoring. http://www.refactoring.com

To keep my code compact & clean I would probably move the block to its own
method, possibly moving a number of methods to their own class, eliminating
the need for the block statement...

Of course if you actually have a *real* need for this, then you could use
any block statement to approximate it as Larry shows...

Hope this helps
Jay



<Lucio H> wrote in message | Is it possible to define a variable in a block in order to make
| it invisible outside that block? For example, in C I can write
|
| {
| int a
| ...
| }
|
| then a will only be available inside the curley brackets
|
| In Pascal there is 'Begin/End'
|
| tnx
|
|
 
Is it possible to define a variable in a block in order to make
it invisible outside that block?

Several responders so far have expressed mild disapproval of the practice.
I disagree. I think it is usually (never say always) good practice to limit
the scope of variables. I avoid the practice in loops, but I do use "for i
as integer = ..." which limits the scope of the loop variable i. The most
useful place for the practice (my opinion) is if-then-else blocks where
processing logic often dictates variables used in the then or else clause but
not in both. By placing Dim statements with complex initializers in such
blocks, some of them may not be executed at all depending on the code
execution path. By placing the Dim's at the top of the sub, you either have
to execute the complex initializers (a possible performance hit) or forego
using some initializers (a loss of language utility for the sake of a
stylistic objection).
 
AMercer,
I agree you should limit the scope of your variables, I normally define &
initialize the variable when I need it. Such as on the For statement, inside
a Loop, or in the If or Else clause.

What I disapproved of is introducing a block (the mythical Block & End Block
statements) for the sake of having acutely scoped variables. As the "need"
for said block may be indicative of too long a method...

Then again I normally code smaller methods in smaller types (classes or
structures).

Hope this helps
Jay

|> Is it possible to define a variable in a block in order to make
| > it invisible outside that block?
|
| Several responders so far have expressed mild disapproval of the practice.
| I disagree. I think it is usually (never say always) good practice to
limit
| the scope of variables. I avoid the practice in loops, but I do use "for
i
| as integer = ..." which limits the scope of the loop variable i. The most
| useful place for the practice (my opinion) is if-then-else blocks where
| processing logic often dictates variables used in the then or else clause
but
| not in both. By placing Dim statements with complex initializers in such
| blocks, some of them may not be executed at all depending on the code
| execution path. By placing the Dim's at the top of the sub, you either
have
| to execute the complex initializers (a possible performance hit) or forego
| using some initializers (a loss of language utility for the sake of a
| stylistic objection).
 
AMercer,
By placing the Dim's at the top of the sub, you either have
to execute the complex initializers (a possible performance hit) or forego
using some initializers (a loss of language utility for the sake of a
stylistic objection).

If you are afraid of possible performance hits about parts of milliseconds
you should in my opinion not use an OOP language however Intel assembler.
With OOP you will never create the most absolute highest performing
programs.

Always placing the declaration in top of a method can give all kind of
trouble that you deny with doing that because you know that if it is placed
there with a reason (A reason can be because it should be used outside of
the scope of the block as you wrote)

If you use variables only consequently in the block scope where they are
needed, than you have less change that an variable is changed wrong inside a
block just because the wrong dataname was used.

A maybe less however as well benefit is that you can use datanames more than
ones and be consequent in that what gives in my opinion a better
documentation.

Just my opinion.

Cor
 
Is it possible to define a variable in a block in order to make
it invisible outside that block? For example, in C I can write

{
int a
...
}

then a will only be available inside the curley brackets

\\\
....
With Nothing
Dim a As Integer
...
End With
....
If True Then
Dim b As Integer
...
End If
....
///

;-)
 

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

Back
Top