データ整合性の問題のC#
-
12-09-2019 - |
質問
私の問題は、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);
}}
解決
クラスのインスタンスを参照として保存されます。
decodedProxyExcerpt1とdecodedProxyExcerpt2は両方とも同じオブジェクトへの参照である - 。audiofactory.CurrentExcerpt
他のヒント
同じ参照に滞在されaudiofactory.MoveNext(stepSize)
その様子から。これはaudiofactory.Current(stepSize)
が同じアドレスにご滞在を引き起こしている。
この理由が、一方が他方に伝搬することdecodedProxyExcerpt1
と同じ基準にdecodedProxyExcerpt2
点とが変化する。
だから、問題はあなたのAudioFactory
クラスにあります。
私は私に私がCに考えていたかもしれない++の配列の割り当てではなく、配列の割り当ては、参照を作成するC#のの、コピーを作成する場所のヒントを与えた誰このことについて友人に尋ねます。
それが正しいの場合と
decodedProxyExcerpt1 = audiofactory.Current(ステップサイズ)
次に、上書きが完全に理解される基準(ないコピー)を設定されます。
所属していません StackOverflow