rsgmon
rsgmon

Reputation: 1932

Why are members of the list in the base class being changed?

This is a simplified example to illustrate my question. I'll explain after this code.

class Payment
{
    private double amount = 1;
    private DateTime date = new DateTime(2000, 01, 01);
    public Payment() {}
    public double Amount { get { return amount; } 
        set { this.amount = value; } }
    public DateTime Date { get { return date; } }
}

class PaymentStream
{
    public List<Payment> paymentStream = new List<Payment>();
    public PaymentStream()
    {
        for (int i = 0; i < 1; i++)
            paymentStream.Add(new Payment());
    }
    public Payment getIndividualPayment(int index) 
    { return paymentStream[index]; }

    public void displayStream()
    {
        string displayDetails = "Amount {0:c}   Date{1,10:d} ";
        foreach (Payment i in paymentStream)
        {
            Console.WriteLine(displayDetails, i.Amount, i.Date);
        }
    }
}
class DoubledPaymentStream : PaymentStream
{
    public List<Payment> doubledPaymentStream = new List<Payment>();
    public DoubledPaymentStream () : base()
    {
        for (int i = 0; i < 1; i++)
        {
            doubledPaymentStream.Add(getIndividualPayment(i));
            doubledPaymentStream[i].Amount = 
                doubledPaymentStream[i].Amount * 2;
        }
    }
    public Payment getIndividualDoubledPayment(int index)
    { return doubledPaymentStream[index]; }
}
class Program
{
    static void Main(string[] args)
    {
        DoubledPaymentStream doubledstream = new DoubledPaymentStream();
        Console.WriteLine(doubledstream.paymentStream[0].Amount);
        Console.WriteLine(doubledstream.doubledPaymentStream[0].Amount);
    }
}

So its pretty basic. I've created:

In my mind when I create a DoublePaymentStream object I will have two lists: paymentStream and doubledPaymentStream. Each list should be independent of the other. The amounts of doubledPaymentStream should be twice the value of paymentStream.

What's happening though is the values in paymentStream are being doubled when they shouldn't. So the values in paymentStream end being the same as doubledPaymentStream.

I think it probably has something to do with DoubledPaymentStreams constructor that calls a method in PaymentStream and somehow that is being reassigned.

Can you please explain what I've done incorrectly and what I need to change to fix this?

Thank You!

Upvotes: 0

Views: 74

Answers (3)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73492

I don't have to explain you the underlying problem, if you read about Value Types and Reference Types you'll come to know you're basically modifying the same reference and hence both the lists are updated.

So, now we know what's the problem. How to fix it? Just make the class as struct or add a copy of instance to doubledPaymentStream. That should solve your problem.

Upvotes: 1

Asik
Asik

Reputation: 22123

That would be because when you do:

doubledPaymentStream.Add(getIndividualPayment(i));

You're actually adding the original Payment object to another list. Both lists now have a reference to the same object, and when you modify it, it changes in both lists.

You either need to create separate Payment objects to put in your new list, or make Payment a value type.

Upvotes: 3

Juan Rada
Juan Rada

Reputation: 3786

Proably your are using the same class istance and class intance are passed as references in c#

so if you for example

Payment p = new Payment();
p.amount = 10;
paymentStream.add(p);

and then

p.amount = 2*p.amount;
doubledPaymentStream.add(p); 

both list will contain the same payment with ammount 20.

Upvotes: 0

Related Questions