473,657 Members | 2,566 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 1321


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*******@hotm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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******** *****@TK2MSFTNG P15.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******** *****@TK2MSFTNG P15.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*****@discus sions.microsoft .com> wrote in message
news:DD******** *************** ***********@mic rosoft.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

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

Similar topics

23
5080
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), where I defined various variables that are used throughout the site. I could then access all of these in any other file by simply including that file (using #include), then referencing the variables. I have been unable to do the same thing in...
2
2032
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 the finally block is existing there cuz of the logic inside try block, if try is not there finally wont be there. If try and finally are so closely related than finally should share the scope of try. Please explain the rational behind this scope...
2
3595
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
2801
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 recent thread. I want to know why this descrimination is in place. Can't all the variables be initialised to NULL automatically by the compiler? This would make programming a little easier.
44
3609
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
3218
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: /****************************************************/ /* ... usual includes and such */ int var = 1; if(var = 1){ printf("hello world\n");
6
3219
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 automatic non volatile variables of the function invoking setjmp, these will be undefined if modified after the setjmp call"
4
6733
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
1874
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, ); *m_image = newImage; // m_image is a QImage }
0
8397
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7333
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6167
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
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 we have to send another system
2
1620
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.