Solved

How to update the static variable value in C#?

Posted on 2014-11-05
2
4,423 Views
Last Modified: 2014-11-05
Is it possible to change the static variable value in C#?

Thanks,
D.
0
Comment
Question by:developer2012
2 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40424733
Yes, unless the static variable is marked as readonly or it's property used to expose it is set as private set or has no setter method.

Example -
public class StaticVariables
{
	private readonly static string readOnlyStatic = "I am a read-only static variable.";
	private static string propertySetStatic = "I am a property set accessor static variable.";
	private static string propertyPrivateSetStatic = "I am a property with private set accessor static variable.";
	private static string propertyNoSetStatic = "I am a property with no set accessor static variable.";

	public static string ReadOnlyStatic
	{
		get { return readOnlyStatic; }
		set { readOnlyStatic = value; }
	}

	public static string PropertySetStatic
	{
		get { return propertySetStatic; }
		set { propertySetStatic = value; }
	}

	public static string PropertyPrivateSetStatic
	{
		get { return propertyPrivateSetStatic; }
		private set { propertyPrivateSetStatic = value; }
	}

	public static string PropertyNoSetStatic
	{
		get { return propertyNoSetStatic; }
	}
}

public class Program
{
	public static void Main()
	{
		Console.WriteLine("My static settings");
		Console.WriteLine(StaticVariables.ReadOnlyStatic);
		Console.WriteLine(StaticVariables.PropertySetStatic);
		Console.WriteLine(StaticVariables.PropertyPrivateSetStatic);
		Console.WriteLine(StaticVariables.PropertyNoSetStatic);
		StaticVariables.ReadOnlyStatic = "This cannot be set, it is marked as private.  The value can only be changed by the constructor.";
		StaticVariables.PropertySetStatic = "This can be changed, it has a public property which exposes the backing field.";
		StaticVariables.PropertyPrivateSetStatic = "This cannot be set, it has a public property but the property has a private set accessor.  " +
			"As such the static class can modify the backing field but nothing outside the static class can access it's value.";
		StaticVariables.PropertyNoSetStatic = "The rules for PropertyPrivateSetStatic apply to this static.";
		Console.WriteLine();
		Console.WriteLine("My new static settings");
		Console.WriteLine(StaticVariables.ReadOnlyStatic);
		Console.WriteLine(StaticVariables.PropertySetStatic);
		Console.WriteLine(StaticVariables.PropertyPrivateSetStatic);
		Console.WriteLine(StaticVariables.PropertyNoSetStatic);
		// Wait for user input to close the program.
		Console.ReadLine();
	}
}

Open in new window


I get the following compile time errors:Read-Only static errorProperty private set static errorProperty with no set static error
Once I correct the issues -
using System;
using System.Text;

public class StaticVariables
{
	private readonly static string readOnlyStatic = "I am a read-only static variable.";
	private static string propertySetStatic = "I am a property set accessor static variable.";
	private static string propertyPrivateSetStatic = "I am a property with private set accessor static variable.";
	private static string propertyNoSetStatic = "I am a property with no set accessor static variable.";

	public static string ReadOnlyStatic
	{
		get { return readOnlyStatic; }
		//set { readOnlyStatic = value; }
	}

	public static string PropertySetStatic
	{
		get { return propertySetStatic; }
		set { propertySetStatic = value; }
	}

	public static string PropertyPrivateSetStatic
	{
		get { return propertyPrivateSetStatic; }
		private set { propertyPrivateSetStatic = value; }
	}

	public static string PropertyNoSetStatic
	{
		get { return propertyNoSetStatic; }
	}
}

public class Program
{
	public static void Main()
	{
		Console.WriteLine("My static settings");
		Console.WriteLine(StaticVariables.ReadOnlyStatic);
		Console.WriteLine(StaticVariables.PropertySetStatic);
		Console.WriteLine(StaticVariables.PropertyPrivateSetStatic);
		Console.WriteLine(StaticVariables.PropertyNoSetStatic);
		//StaticVariables.ReadOnlyStatic = "This cannot be set, it is marked as private.  The value can only be changed by the constructor.";
		StaticVariables.PropertySetStatic = "This can be changed, it has a public property which exposes the backing field.";
		//StaticVariables.PropertyPrivateSetStatic = "This cannot be set, it has a public property but the property has a private set accessor.  " +
		//     "As such the static class can modify the backing field but nothing outside the static class can access it's value.";
		//StaticVariables.PropertyNoSetStatic = "The rules for PropertyPrivateSetStatic apply to this static.";
		Console.WriteLine();
		Console.WriteLine("My new static settings");
		Console.WriteLine(StaticVariables.ReadOnlyStatic);
		Console.WriteLine(StaticVariables.PropertySetStatic);
		Console.WriteLine(StaticVariables.PropertyPrivateSetStatic);
		Console.WriteLine(StaticVariables.PropertyNoSetStatic);
		// Wait for user input to close the program.
		Console.ReadLine();
	}
}

Open in new window


I get this output:Capture.JPG
One word of caution.  Static variables are not instance variables, if you change the value of a static variable, all references to the static variable will produce the new value.

-saige-
0
 

Author Closing Comment

by:developer2012
ID: 40424864
Thanks, it_saige it gave me an innter view what I am looking for !!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question