Solved

How to update the static variable value in C#?

Posted on 2014-11-05
2
3,780 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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
Thanks, it_saige it gave me an innter view what I am looking for !!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now