2009-04-21 57 views
1

我的問題是,下面decodedProxyExcerpt2的分配覆蓋decodedProxyExcerpt1,我不知道爲什麼。數據完整性問題C#

任何線索?

在此先感謝。

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

是不是真的有足夠的信息在這裏知道發生了什麼 - 你的問題很可能是內部audioFactory。 – cjk 2009-04-21 09:58:44

回答

1

類的實例存儲爲引用。

decodedProxyExcerpt1和decodedProxyExcerpt2是同一個對象都引用 - audiofactory.CurrentExcerpt。

4

從它audiofactory.MoveNext(stepSize)停留在相同的長相。這導致audiofactory.Current(stepSize)留在相同的地址。

由於這個原因,但decodedProxyExcerpt1decodedProxyExcerpt2指向相同的參考,因此更改爲一個傳播到另一個。

所以,問題在於你AudioFactory類。

0

我問一個朋友關於誰給我說,我會一直在思考,而不是C#,其中一個數組分配創建了一個參考在C++當陣列中分配創建副本的提示。

如果這是正確的,

decodedProxyExcerpt1 = audiofactory.Current(stepSize的);

被設定基準(不是複製),則重寫是完全可以理解。