2016-03-23 1 views
0

私は文字列分割関数を実行するかどうかを決定するためにVBフォームでチェックボックスを選択しています。私は6つの文字列フィールドを持っています。これらのフィールドには、カンマ区切りの値が含まれています。複数のString.Split Intersectionを実行してリストに格納できる文字列として使用できますか?

私の現在のオプションは、それぞれの可能な条件についてElseを使用しますが、6つのフィールドがあるので、コードに2^6の結果が必要です。私は現在のアプローチのアイデアを与えるために2つのフィールドのスニペットを提供しています。

Dim masterFormList = "AAA,BBB,CCC,FFF,GGG,HHH" 
    Dim otherList = "XXX,BBB,YYY,AAA" 

    Dim r = 0 'will replace these using checkboxes 
    Dim k = 1 'will replace these using checkboxes 
    Dim c As Char() = New Char() {","c} 

    Dim matches = 
    If(r = 1, 
     (If(k = 1, 
    otherList.Split(c).Intersect(masterFormList.Split(c)).ToList(), 
    otherList.Split(c))), 
    (If(k = 1, 
     masterFormList.Split(c).ToList(), 
    Nothing))) 

私はこれを行うための簡単な方法があるかどうかを知りたいです。

+0

omg、そのif文... –

+0

笑。私は64例をどうやってやるのか考えていました。 –

答えて

0

あなたがしたいことを理解しているかどうかはわかりません。あなたは他のリストの中にあなたのリストを入れます。

Dim combinedList As New List(Of String) 
    combinedList.Add(masterFormList) 
    combinedList.Add(otherList) 

わかっていれば、rとkは比較したいリストのインデックスを表します。

Dim matches = combinedList(r).Split(c).Intersect(combinedList(k).Split(c)).ToList() 

2つ以上のリストを交差させる必要がある場合は、順番にそれを行う必要があります。

Dim matches = combinedList(r).Split(c) 
    matches = matches.Intersect(combinedList(k).Split(c)).ToList() 
    matches = matches.Intersect(combinedList(z).Split(c)).ToList() 
    ... 
+0

これはチェックボックスの選択です。チェックボックスの選択がtrueの場合フォームでチェックされているリストのみの交差を望みます。 –

+0

しかし、あなたのロジックは物事を良くするだろう。与えられた例では、ジョブを実行するのは2つの条件だけです。 –

+0

@ MetaNaamParkerもしあなたがより多くのリストを一緒に交差させたいならば、それらを順々に行います(これは、r、k、...がリスト内にあればループで実行できます) –

0

返信いただきありがとうございます。返信いただきありがとうございます。返信いただきありがとうございます。私は最後の数日間旅行していました。しかし、あなたの提案は完璧に機能し、私が思っていたよりもずっと簡単になりました。私はまだプログラミングのノブです。かなり素早く物事のハングを取得しています。私が最終的に使い終わったコードを投稿する。

For Each row As DataRow In ds.Tables(0).Rows 
      If box1.checked Then 
          If IsDBNull(row.Item("Column1")) = False Then 
          CombinedList.Add(row.Item("Column1")) 
          End If 
        End If 
      If box2.Checked Then 
          If IsDBNull(row.Item("Column2")) = False Then 
          CombinedList.Add(row.Item("Column2")) 
          End If 
      End If 
        If box3.checked Then 
        If IsDBNull(row.Item("Column3")) = False Then 
            CombinedList.Add(row.Item("Column3")) 
          End If 
      End If 
        If box4.checked Then 
          If IsDBNull(row.Item("Column4")) = False Then 
            CombinedList.Add(row.Item("Column4")) 
          End If 
      End If 
        If box5.checked Then 
        If IsDBNull(row.Item("Column5")) = False Then 
            CombinedList.Add(row.Item("Column5")) 
          End If 
      End If 
        If CombinedList.Count > 0 Then 
       Dim ConsolidatedListArr = CombinedList(0).Split(c) 
         Dim CommonListArr = CombinedList(0).Split(c) 
        For Each item In CombinedList 
           ConsolidatedClientsArr = ConsolidatedClientsArr.Union(item.Split(c)).ToArray() 
           CommonClientsArr = CommonClientsArr.Intersect(item.Split(c)).ToArray() 
        Next 
         Consolidatedstring = String.Join(",", ConsolidatedClientsArr.ToArray()) 
       CommonString = String.Join(",", CommonClientsArr.ToArray()) 
        End If 
    Next 
関連する問題