?
Solved

How to update the static variable value in C#?

Posted on 2014-11-05
2
Medium Priority
?
5,318 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
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.
Suggested Courses

765 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