1

私の問題は、以下のdecodedProxyExcerpt2の割り当てがdecodedProxyExcerpt1を上書きすることであり、その理由はわかりません。

手がかりはありますか?

前もって感謝します。

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

}

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

3 に答える 3

1

クラスのインスタンスは参照として保存されます。

decodedProxyExcerpt1 と decodedProxyExcerpt2 はどちらも同じオブジェクト (audiofactory.CurrentExcerpt) への参照です。

于 2009-04-21T11:57:55.490 に答える
0

これについて友人に尋ねたところ、配列の代入によって参照が作成される C# ではなく、配列の代入によってコピーが作成される C++ で考えていた可能性があるというヒントが得られました。

それが正しければ、そして

decodedProxyExcerpt1 = audiofactory.Current(stepSize);

参照(コピーではない)を設定している場合、上書きは完全に理解できます。

于 2009-04-21T11:19:16.223 に答える