Solved

How to update the static variable value in C#?

Posted on 2014-11-05
2
4,655 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

726 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