2016-05-30 75 views
-1

我正在查找代碼來檢查我的double數組的元素是否爲空。我試着用isNaN,string.isNullOrEmpty0.0D但沒有什麼可做的,我仍然有這樣的文字:不是數字。檢查雙c的空元素數組#

那麼,你知道C#中的任何代碼,它能夠檢查一個double數組中的元素是否爲空?

這裏是我的代碼:

if (!Double.IsNaN(d1.getChiffreAffaireBase()[keyIndex1])) 
{ 
    textBox43.Text = calcMargeCa(d1.getChiffreAffaireBase()[keyIndex1], d1.getChiffreAffairePlus()[keyIndex1]).ToString("0.00"); 
    textBox44.Text = calcMargeCa(d1.getChiffreAffaireBase()[keyIndex1+1], d1.getChiffreAffairePlus()[keyIndex1+1]).ToString("0.00"); 
} 
else 
{ 
    label13.Hide(); 
    textBox43.Hide(); 
    textBox44.Hide(); 
} 
+6

你是什麼意思* empty *? 'double'是一個ValueType,它不能是「空的」。 –

+3

發佈您的代碼,然後我們會更好地瞭解您要做什麼。 – Nasreddine

+0

我有double [] array = new double [12],我想檢查例如array [6]是否沒有值。 –

回答

4

Value types不能沒有值(也就是說,他們不能null),但你可以使用Nullable<double>(或簡稱爲double?),它可以如果您想指示它沒有值,請設置爲null。這裏有一個例子:

double?[] array = new double?[12]; 

然後檢查是否有一個價值,你把它比作null

if (array[6] == null){ 
    // do your thing 
} 

編輯:

題外話,但現在你已經張貼您的代碼我發現你使用double來表示錢(Chiffre d'Affaire或Revenue),但這可以起作用,你應該use decimal instead

+0

@BlueMonkMN感謝您的重要編輯。 – Nasreddine

5

如果你聲明這樣

double[] array = new double[12]; // elements cannot be null 

所有元素將被設置爲0數組。

聲明爲nullable,如果你真的想

var array = new double?[12]; // elements can be null 
var isEmpty = (array[6] == null); 

var isEmpty = !array[6].HasValue; 
1

Double.IsNaN不檢查空值,檢查這更多或讀到它 的單證https://stackoverflow.com/a/7967190/6001737

相反,您可以使用double?[] array = new double?[12];這是Nullable,然後您可以檢查某個數組值等於零