C# incrementing static variables upon instantiation

安稳与你 提交于 2019-12-08 03:12:04

问题


I have a bankAccount object I'd like to increment using the constructor. The objective is to have it increment with every new object the class instantiates.

Note: I've overriden the ToString() to display the accountType and accountNumber;

Here is my code:

public class SavingsAccount
{
    private static int accountNumber = 1000;
    private bool active;
    private decimal balance;

    public SavingsAccount(bool active, decimal balance, string accountType)
    {
        accountNumber++;
        this.active = active;
        this.balance = balance;
        this.accountType = accountType;
    }
}

Why is it that when I plug this in main like so:

class Program
{
    static void Main(string[] args)
    {
        SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
        SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
        Console.WriteLine(potato.ToString());
        Console.WriteLine(magician.ToString());
    }
}

The output I get does not increment it individually i.e.

savings 1001
savings 1002

but instead I get:

savings 1002
savings 1002

How do I make it to be the former and not the latter?


回答1:


Because a static variable is shared among all instances of the class. What you want is a static variable to keep the global count and a non-static variable to save the current count at the time of instantiation. Change your code above to:

public class SavingsAccount
{
    private static int accountNumber = 1000;
    private bool active;
    private decimal balance;
    private int myAccountNumber;

    public SavingsAccount(bool active, decimal balance, string accountType)
    {
        myAccountNumber = ++accountNumber;
        this.active = active;
        this.balance = balance;
        this.accountType = accountType;
    }
}

class Program
{
    static void Main(string[] args)
    {
        SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
        SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
        Console.WriteLine(potato.ToString());
        Console.WriteLine(magician.ToString());
    }
}

And then in your ToString() overload you should print myAccountNumber instead of the static variable.




回答2:


Because it is a static variable. It is shared by all instances of the class. You need to save off the incremented value to an instance variable.

public class SavingsAccount
{
    private static int accountNumberCounter = 1000;
    private int accountNumber;
    private bool active;
    private decimal balance;

    public BankAccount(bool active, decimal balance, string accountType)
    {
        accountNumberCounter++;
        this.accountNumber = accountNumberCounter;

        this.active = active;
        this.balance = balance;
        this.accountType = accountType;
    }

    public string ToString() 
    {
        return String.Format("{0} {1}", accountType, accountNumber);
    }
}



回答3:


You have declared the variable account as static which means its instantiated at class level and not instance level. So when you do the increment it happens two times for when one variable.

The possible way to achieve what you want is to insert the print command between the two.




回答4:


Try this:

public class SavingsAccount
{
    private static int accountNumberMarker = 1000;
    private int accountNumber;
    private bool active;
    private decimal balance;

    public SavingsAccount(bool active, decimal balance, string accountType)
    {
        accountNumber = ++accountNumberMarker;
        this.active = active;
        this.balance = balance;
        this.accountType = accountType;
    }
}



回答5:


you can try

        SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
        Console.WriteLine(potato.ToString());
        SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
        Console.WriteLine(magician.ToString());

then , you could get you want.

The static variable has only one copy in entire runtime. No matter how many times created the class's instance , the variable is referring to the same memory location.




回答6:


Because a static is shared with all members in the class?

You want a static variable like you have now for a global number that you can increment, but you also want a private variable specific to that account. Hence, you should add this:

private int thisAccountNumber;

...to the class definition, and modify your existing line in the constructor to read:

thisAccountNumber = accountNumber++;

Then use thisAccountNumber as you will.



来源:https://stackoverflow.com/questions/8132413/c-sharp-incrementing-static-variables-upon-instantiation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!