Search code examples
.netvb.netlistcsvsubroutine

Vb.net two comma separated lists


I am being passed two strings that contain comma separated lists.

string1 (1,2,3,4)
String2 (Red, blue, yellow, purple)

I want to pass each pair to another routine to process them. The strings can change along with the index.

I have to split each string value and loop through both so that I could pass each pair to a subroutine


Solution

  • Use Split to get each component between the commas and pass this:

        Dim string1 As String = "1,2,3,4"
        Dim string2 As String = "Red,blue,yellow,purple"
    
        Dim string1AsArray = string1.Split(","c)
        Dim string2AsArray = string2.Split(","c)
    
        For i = 0 To string1AsArray.Count - 1
            MyRoutine(string1AsArray(i), string2AsArray(i))
        Next
    

    you may want to check that both of the arrays are the same size otherwise you may get an exception thrown