views:

189

answers:

3

Hi,

my problem is that the assignment of decodedProxyExcerpt2 below overwrites decodedProxyExcerpt1 and I do not know why.

Any clues?

Thanks in advance.

        DecodedProxyExcerpt decodedProxyExcerpt1 = new DecodedProxyExcerpt(stepSize);
        if (audiofactory.MoveNext(stepSize))
        {
            decodedProxyExcerpt1 = audiofactory.Current(stepSize);
        }
        // At this point decodedProxyExcerpt1.data contains the correct values.

        DecodedProxyExcerpt decodedProxyExcerpt2 = new DecodedProxyExcerpt(stepSize);
        if (audiofactory.MoveNext(stepSize))
        {
            decodedProxyExcerpt2 = audiofactory.Current(stepSize);
        }
        // At this point decodedProxyExcerpt2.data contains the correct values.
        // However, decodedProxyExcerpt1.data is overwritten and now holds the values of decodedProxyExcerpt2.data.


public class DecodedProxyExcerpt
{
    public short[] data { get; set; } // PCM data

    public DecodedProxyExcerpt(int size)
    {
        this.data = new short[size];
    }

}

From AudioFactory:

    public bool MoveNext(int stepSize)
    {
        if (index == -1)
        {
            index = 0;
            return (true);
        }
        else
        {
            index = index + stepSize;
            if (index >= buffer.Length - stepSize)
                return (false);
            else
                return (true);
        }
    }

    public DecodedProxyExcerpt Current(int stepSize)
    {
        Array.Copy(buffer, index, CurrentExcerpt.data, 0, stepSize);
        return(CurrentExcerpt);
    }}
+4  A: 

From the looks of it audiofactory.MoveNext(stepSize) is staying at the same reference. This is causing audiofactory.Current(stepSize) to stay at the same address.

For this reason, but decodedProxyExcerpt1 and decodedProxyExcerpt2 point to the same reference and therefore changes to one propagate to the other.

So, the problems lies in your AudioFactory class.

Jose Basilio
A: 

I asked a friend about this who gave me the hint that I might have been thinking in C++ where assignment of an array creates a copy, instead of C# where assignment of an array creates a reference.

If that is correct and

decodedProxyExcerpt1 = audiofactory.Current(stepSize);

is setting a reference (not a copy) then the overwrite is completely understandable.

Hauke
+1  A: 

Instances of classes are stored as references.

decodedProxyExcerpt1 and decodedProxyExcerpt2 are both references to the same object -- audiofactory.CurrentExcerpt.

Jonathan