Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Is it possible to change the static field value inside different function of same class in c#??Can anyone help to make understand with useful example.Thanks
Posted
Updated 30-Nov-15 6:32am
v2
Comments
Richard MacCutchan 30-Nov-15 12:32pm    
Of course. A static field has the same visibility to class methods as instance fields.
Member 12131509 30-Nov-15 12:49pm    
Thanks for response. But can you please explain with the suitable example.Because I have read in many posts and it was mentioned that then what is the main purpose of using static field if we can solve the same purpose using instance field/variable..

The simple answer is that you can set the value of a Field wherever you have a valid reference to that field.

If you have a public static Field, then wherever you have a valid reference at run-time to the (NameSpace and/or) Class Name it is defined in, you can use it.

If you have a non-static Field declared 'public, you can use it anywhere you have a valid reference to the instance it is defined in.

Of course, all access in .NET is governed by the 'access modifiers used when declaring Fields / writing the code. You make a Field read-only, and you can't set its value, and so forth.

You should study the access modifiers including:

public/private/internal/protected ... and readonly

If you define: public readonly static int Counter = 0;

You can't set it, even if you have access to it, but you can, of course, read its value;
 
Share this answer
 
A static variable allows you to count instances of a class, by adding 1 to the variable each time you pass through the constructor.
C#
class Foo
{
    static int counter;
    foo()
    {
        counter++;
    }

    static void main()
    {
        Foo bar = new Foo();
        Foo poo = new Foo();
        Console.writeline(counter());
   }
}


NB: Did you also post this question: http://www.codeproject.com/Messages/5167614/Using-static-var-on-a-class.aspx[^], under a different user id?
 
Share this answer
 
Comments
Member 12131509 30-Nov-15 13:17pm    
Thanks.But Can I assign the new value to the static variable inside different fuctions os same class, which I have already initialized at class level??
phil.o 30-Nov-15 13:27pm    
Why not trying?
Richard MacCutchan 30-Nov-15 14:27pm    
Yes, I told you this already. And, as phil.o suggested, why not try it? You will be amazed what you can learn by practicing.
Member 12131509 2-Dec-15 12:54pm    
Agreed..Thanks..
BillWoodruff 30-Nov-15 15:21pm    
+5 this solution did not deserve a down-vote !
The question is not clear, by the following reason: "change the static field value inside different function of same class" main mean different things. One understanding of such "inside" situation would be: "There is a static field if a class A. Can I change it in the code another function?" This formulation would be invalid, because there is no "the same" or "another" function. What is "not another"? A field can be defined only in a class, not in a function. If you mean some object defined inside a function, it is never a field. So, the other understanding of this question would be even less valid: "I have a static field defined in one function, can I modify it in another function?" — there is no such thing.

So, the only answer would be: all functions of the same class have the same access to any member of this class. The "static" makes no difference. A static member belongs to the class and not to any particular instance. A static member can be accessed in both static and instance members of a class, but non-static members (they are called instance members all need some instance to operate. For a method, it is done be passing the instance as an implicit method parameters called "this". Static methods don't have it.

If you wanted to ask "can I access a local variable defined in one function, from the code of another function?" (as I already said, it cannot be a field), the answer would be "no". The local variables are created on the stack in each separate stack frame, and a stack frame is discarded after each call. All variables declared in a method leave only inside the call, before return.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900