lorenzoid
lorenzoid

Reputation: 1832

C# incrementing static variables upon instantiation

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?

Upvotes: 3

Views: 13551

Answers (6)

user978122
user978122

Reputation: 5771

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.

Upvotes: 0

shenhengbin
shenhengbin

Reputation: 4294

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.

Upvotes: 0

Abdul Munim
Abdul Munim

Reputation: 19217

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;
    }
}

Upvotes: 0

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13594

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.

Upvotes: 0

Matt Greer
Matt Greer

Reputation: 62057

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);
    }
}

Upvotes: 3

Brandon Moore
Brandon Moore

Reputation: 8790

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.

Upvotes: 11

Related Questions