2014-03-26 5 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 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 

어느 쪽이든을 항상 문자열을 연결하는 문자열 연결 연산자 &을 사용하십시오 :

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 

당신도 고려할 수 있습니다. 산술 가산 연산자 +은 문자열에 사용하면 때때로 문제가 발생할 수 있습니다.

+0

내가 어떤 케이스를 추가하는지 다시 물을 수 있습니까? 당신은 내 문제를 도와 줄 수 내 게시물을 볼 수 – Denmark

+0

그게 당신의 디버거가 무엇입니까. 코드를 단계별로 한 줄씩 살펴보고 그 내용을 확인하십시오. –

관련 문제