473,762 Members | 8,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable scope!

Hey

Below is a C# program I've made.... it's an app where I experiment with
variable scope. In this code you see two variables named i (one is a local
variable and the other is a member of the class). This code print out 5 (
Console.WriteLi ne(i); )... the member variable isn't passed on to the
Multiply, Increment variables etc.. I wonder what the rules are here... I've
been googling and and can't find a good article on variable scope in C#
which explain if a local variable and member variable has the same name like
in this code, so maybe you have a link?

class Program
{
private static int i = 1;

public static void Main(string[] args)
{
int i = 5;
Multiply(i);
Increment(i);
Reset();
Console.WriteLi ne(i);
}

private static void Reset()
{
i = 0;
}

private static void Multiply(int i)
{
i *= 2;
}

public static void Increment(int i)
{
i++;
}
}
Nov 26 '06 #1
5 4796
Hi Jeff,

Method parameters (Multiply(int i)) have preference over members
(private static int i = 1)

In this case, the "i" parameter hides the "i" member, and since an int
is a value type, the contents are copied.

In the increment() and multiply() the results are lost.

you remove the parameters from the method:
private static void Multiply()
{
i *= 2;
}

public static void Increment()
{
i++;
}

Hey

Below is a C# program I've made.... it's an app where I experiment with
variable scope. In this code you see two variables named i (one is a local
variable and the other is a member of the class). This code print out 5 (
Console.WriteLi ne(i); )... the member variable isn't passed on to the
Multiply, Increment variables etc.. I wonder what the rules are here... I've
been googling and and can't find a good article on variable scope in C#
which explain if a local variable and member variable has the same name like
in this code, so maybe you have a link?

class Program
{
private static int i = 1;

public static void Main(string[] args)
{
int i = 5;
Multiply(i);
Increment(i);
Reset();
Console.WriteLi ne(i);
}

private static void Reset()
{
i = 0;
}

private static void Multiply(int i)
{
i *= 2;
}

public static void Increment(int i)
{
i++;
}
}

Nov 26 '06 #2
Jeff,
In the function that you have written i.e.
private static void Multiply(int i) and also in the class, you have
private static int i = 1;

In the scope of function Multiply, you see two variable i declaration.

One is a static member while other is a local variable.

As far as i know, local variable has more preference so, the function
was multiplying the local variable than the member variable.

if you want to access the member variable, then you have to resolve the
scope for the compiler and tell it explicitly that you want to access
the member static member variable.

you do that by saying Program.i = i * 5;

-Prakash.

Jeff wrote:
Hey

Below is a C# program I've made.... it's an app where I experiment with
variable scope. In this code you see two variables named i (one is a local
variable and the other is a member of the class). This code print out 5 (
Console.WriteLi ne(i); )... the member variable isn't passed on to the
Multiply, Increment variables etc.. I wonder what the rules are here... I've
been googling and and can't find a good article on variable scope in C#
which explain if a local variable and member variable has the same name like
in this code, so maybe you have a link?

class Program
{
private static int i = 1;

public static void Main(string[] args)
{
int i = 5;
Multiply(i);
Increment(i);
Reset();
Console.WriteLi ne(i);
}

private static void Reset()
{
i = 0;
}

private static void Multiply(int i)
{
i *= 2;
}

public static void Increment(int i)
{
i++;
}
}

Nov 26 '06 #3
Jeff,
In the function that you have written i.e.
private static void Multiply(int i) and also in the class, you have
private static int i = 1;

In the scope of function Multiply, you see two variable i declaration.

One is a static member while other is a local variable.

As far as i know, local variable has more preference so, the function
was multiplying the local variable than the member variable.

if you want to access the member variable, then you have to resolve the
scope for the compiler and tell it explicitly that you want to access
the member static member variable.

you do that by saying Program.i = i * 5;

-Prakash.

Jeff wrote:
Hey

Below is a C# program I've made.... it's an app where I experiment with
variable scope. In this code you see two variables named i (one is a local
variable and the other is a member of the class). This code print out 5 (
Console.WriteLi ne(i); )... the member variable isn't passed on to the
Multiply, Increment variables etc.. I wonder what the rules are here... I've
been googling and and can't find a good article on variable scope in C#
which explain if a local variable and member variable has the same name like
in this code, so maybe you have a link?

class Program
{
private static int i = 1;

public static void Main(string[] args)
{
int i = 5;
Multiply(i);
Increment(i);
Reset();
Console.WriteLi ne(i);
}

private static void Reset()
{
i = 0;
}

private static void Multiply(int i)
{
i *= 2;
}

public static void Increment(int i)
{
i++;
}
}

Nov 26 '06 #4
Jeff,
In the function that you have written i.e.
private static void Multiply(int i) and also in the class, you have
private static int i = 1;

In the scope of function Multiply, you see two variable i declaration.

One is a static member while other is a local variable.

As far as i know, local variable has more preference so, the function
was multiplying the local variable than the member variable.

if you want to access the member variable, then you have to resolve the
scope for the compiler and tell it explicitly that you want to access
the member static member variable.

you do that by saying Program.i = i * 5;

-Prakash.

Jeff wrote:
Hey

Below is a C# program I've made.... it's an app where I experiment with
variable scope. In this code you see two variables named i (one is a local
variable and the other is a member of the class). This code print out 5 (
Console.WriteLi ne(i); )... the member variable isn't passed on to the
Multiply, Increment variables etc.. I wonder what the rules are here... I've
been googling and and can't find a good article on variable scope in C#
which explain if a local variable and member variable has the same name like
in this code, so maybe you have a link?

class Program
{
private static int i = 1;

public static void Main(string[] args)
{
int i = 5;
Multiply(i);
Increment(i);
Reset();
Console.WriteLi ne(i);
}

private static void Reset()
{
i = 0;
}

private static void Multiply(int i)
{
i *= 2;
}

public static void Increment(int i)
{
i++;
}
}

Nov 26 '06 #5
Expanding on the above.
If you want to see how to pass integers as reference types, then see this
post:
http://msdn.microsoft.com/newsgroups...0af&sloc=en-us

--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
"Wiebe Tijsma" wrote:
Hi Jeff,

Method parameters (Multiply(int i)) have preference over members
(private static int i = 1)

In this case, the "i" parameter hides the "i" member, and since an int
is a value type, the contents are copied.

In the increment() and multiply() the results are lost.

you remove the parameters from the method:
private static void Multiply()
{
i *= 2;
}
>
public static void Increment()
{
i++;
}


Hey

Below is a C# program I've made.... it's an app where I experiment with
variable scope. In this code you see two variables named i (one is a local
variable and the other is a member of the class). This code print out 5 (
Console.WriteLi ne(i); )... the member variable isn't passed on to the
Multiply, Increment variables etc.. I wonder what the rules are here... I've
been googling and and can't find a good article on variable scope in C#
which explain if a local variable and member variable has the same name like
in this code, so maybe you have a link?

class Program
{
private static int i = 1;

public static void Main(string[] args)
{
int i = 5;
Multiply(i);
Increment(i);
Reset();
Console.WriteLi ne(i);
}

private static void Reset()
{
i = 0;
}

private static void Multiply(int i)
{
i *= 2;
}

public static void Increment(int i)
{
i++;
}
}
Nov 26 '06 #6

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

Similar topics

7
2677
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return before the variable is used? I'm trying to get an idea of whether the .NET compilers for VB.NET and C# will move all variable declaration to the beginning of a method or whether they will allocate the memory as it is needed by the method to...
3
2017
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2 + el2; if (value1 > value2) return 1;
4
14905
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
23
19203
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
1
25697
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
0
35245
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
2
4716
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
5
2238
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;
112
5471
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
3
2047
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in the same file (.c + all relevant .h's)? e.g.
0
9554
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
9378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9927
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
9812
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7360
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
6640
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5268
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2788
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.