2014-03-26 47 views
0

我想將「zone_check_value」存儲到字符串數組中,然後在插入數組時我將檢查字符串數組,如果下一個值被重複或重複。使用For Each循環的數組的存儲列表

例子。

1st Example 
    1st loop = zone_check_value = ZD1/01/2014 
    2nd loop = zone_check_value = ZD1/01/2014 
2nd Example 
    1st loop = zone_check_value = ZD1/01/2014 
    2nd loop = zone_check_value = ZD2/02/2014 
    3rd loop = zone_check_value = ZD1/01/2014 

代碼:

For Each dt As DataTable In xls.Tables 
    Dim array_of_string as String() 'i want to put the value in here 
    For Each dr As DataRow In dt.Rows 
     Dim zone_destination As String = dr(2).ToString 
     Dim affected_date As String = dr(7).ToString 
     Dim zone_check_value = zone_destination + affected_date 
     ''''''How can i store zone_check_value in a string array? 
    Next 
Next 

編輯

如果我在我的循環加選擇呢? array_of_string的值變爲NULL。我需要檢查array_of_string中當前值的值。

示例代碼

For Each dt As DataTable In xls.Tables 
    Dim array_of_string as String() 'i want to put the value in here 
    select Case dt.tablename 
    case "Sheet1" 
     For Each dr As DataRow In dt.Rows 
     Dim zone_destination As String = dr(2).ToString 
     Dim affected_date As String = dr(7).ToString 
     Dim zone_check_value = zone_destination + affected_date 
     ''''''How can i store zone_check_value in a string array? 
     Next 
    case "Sheet 2" 
     For Each dr As DataRow In dt.Rows 
     Dim check_value as Boolean = array_of_string.Contains(dr(0).ToString) 
     'but when i got in sheet 2 the array_of_string is null 
     Next 
Next 
+0

你已經用vb.net和vba標記了這個問題。請選擇一個或另一個。 –

+0

抱歉@ Dan-o現在我已經編輯了它 – Denmark

回答

0

最簡單的方法是使用一個List(Of String)

For Each dt As DataTable In xls.Tables 
    Dim array_of_string as List(Of String) = New List(Of String) 'i want to put the value in here 
    For Each dr As DataRow In dt.Rows 
     Dim zone_destination As String = dr(2).ToString 
     Dim affected_date As String = dr(7).ToString 
     Dim zone_check_value = zone_destination & affected_date 
     ''''''How can i store zone_check_value in a string array? 
     array_of_string.Add(zone_check_value) 
    Next 
    ''' Now if you really need it in array form you can cast it via: 
    ''' Dim values() As String = array_of_string.ToArray() 
Next 

你甚至可以考慮:

For Each dt As DataTable In xls.Tables 
    Dim values As List(Of String) = New List(Of String) 
    dt.Rows.ForEach(Sub(item) values.Add(item(2).ToString & item(7).ToString)) 
    ''' Now do something with values. 
Next 

無論哪種方式,確保你總是使用字符串連接運算符&連接字符串。如果在字符串上使用算術加法運算符+,則會不時出現問題。

+0

我可以再問一次我添加的選擇情況嗎?你可以看到我的文章,你能幫我解決我的問題 – Denmark

+0

這就是你的調試器的用途。逐行瀏覽你的代碼,看看它在做什麼。 –