472,993 Members | 3,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

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.WriteLine(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.WriteLine(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 4756
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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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
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...
3
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...
4
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...
23
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
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...
0
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...
2
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...
5
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
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...
3
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.