473,503 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defining variables in a block?

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
Nov 21 '05 #1
11 1313


LucioH wrote:
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
--
Larry Lard
Replies to group please

Nov 21 '05 #2
(replying to self)

Larry Lard wrote:
LucioH wrote:
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


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

With Nothing
....
End With



--
Larry Lard
Replies to group please


Nov 21 '05 #3
Lucio,

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

I hope this helps,

Cor
Nov 21 '05 #4
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" <la*******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
(replying to self)

Larry Lard wrote:
LucioH wrote:
> 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


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

With Nothing
...
End With



--
Larry Lard
Replies to group please

Nov 21 '05 #5
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...


--
Salute by the First Time!
"Lucio H" wrote:
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

Nov 21 '05 #6
<Lucio H> wrote in message news:O4*************@TK2MSFTNGP15.phx.gbl...
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.
Nov 21 '05 #7
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 news:O4*************@TK2MSFTNGP15.phx.gbl...
| 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
|
|
Nov 21 '05 #8
> 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).
Nov 21 '05 #9
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

"AMercer" <AM*****@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.com...
|> 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).
Nov 21 '05 #10
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
Nov 21 '05 #11
<Lucio H> schrieb:
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
....
///

;-)

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #12

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

Similar topics

23
5052
by: Mark Parnell | last post by:
I'm relatively new to PHP, and have just converted a site from ASP to PHP. There is one thing I haven't managed to do, though. When the site was using ASP, I had one file (called variables.asp),...
2
2027
by: Abubakar | last post by:
Hi all, The variables declared inside try block cant be accessed inside its corresponding finally block. When I used to explain finally concept to anyone I would usually tell them (briefly) that...
2
3588
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
27
2778
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...
44
3578
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
18
3205
by: noridotjabi | last post by:
Whilest I was browesing a tutorial today I came across the infromation that in standard C variables must be decalred at the beginning of a block. Meaning: ...
6
3205
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
4
6725
by: Hans | last post by:
Hi, I want to define a couple of constant strings, like in C: #define mystring "This is my string" or using a const char construction. Is this really not possible in Python? Hans
7
1858
by: Dougan | last post by:
I've seen code that allocates an object on the stack and then saves a class reference to it. example: void ScribbleArea::resizeImage( const QSize &newSize) { QImage newImage( newSize, );...
0
7093
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
7287
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
7348
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...
1
7006
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
7467
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...
1
5021
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...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.