2013-07-23 118 views
0

所以我想從一個litview子項中獲取ByteCount,但它總是顯示負值錯誤: [參數OutOfRangeException未處理:'count'不能爲負,參數名稱:算上] 這是代碼行,我得到的錯誤:GetByteCount顯示爲負值,但它不是

bw.Write(enc.GetBytes(listView1.Items[i].SubItems[3].Text + (new string('\0',dbytecnt - enc.GetByteCount(listView1.Items[i].SubItems[3].Text))))); 

我想這:

if (enc.GetByteCount(listView1.Items[i].SubItems[3].Text) > 0) 
       { 
        bw.Write(enc.GetBytes(listView1.Items[i].SubItems[3].Text + (new string('\0',dbytecnt - enc.GetByteCount(listView1.Items[i].SubItems[3].Text))))); 
       } 

無法正常工作,甚至提出如果條件是「> -1」,相同的結果。

回答

1

參數count上的例外是ArgumentOutOfRangeException。這可能是拋出這個異常的字符串構造函數,而不是GetByteCount

這意味着減法dbytecnt - enc.GetByteCount(listView1.Items[i].SubItems[3].Text)的結果是否定的。

試試這個:

if (dbytecnt - enc.GetByteCount(listView1.Items[i].SubItems[3].Text) > 0) 
{ 
    bw.Write(enc.GetBytes(listView1.Items[i].SubItems[3].Text + (new string('\0',dbytecnt - enc.GetByteCount(listView1.Items[i].SubItems[3].Text))))); 
} 
+0

謝謝你,taht是我所需要的:d – Omarrrio