mehran marandi mehran marandi - 3 months ago 14
C# Question

modify public string with default value in C#

I have an string like "0000000"
and declared it in a class

public class Days_string
{
private string days= "0000000";
public string Days
{
get
{
return days;
}
set
{
days = value;
}
}
}


and I tried to change the string by clicking on 7 buttons
like this:

Days_string daystr = new Days_string();
var aStringBuilder = new StringBuilder(daystr.Days);
aStringBuilder.Remove(5, 1);
aStringBuilder.Insert(5, "1");
daystr.Days = aStringBuilder.ToString();


the output is 0000010

but it changed to 0000000 when I call it again

whats should i do?

Answer

Use static variable and static properties instead.and access the properties using className.properties name

public class Days_string
{
    private static string days = "0000000";
    public static string Days
    {
        get
        {
            return days;
        }
        set
        {
            days = value;
        }
    }
}