Solved

How to update the static variable value in C#?

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

Thanks,
D.
0
Comment
Question by:developer2012
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

696 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