0

I'm working on something relatively simple (or so I thought) and need a bit of help.

I am trying to create a dynamic amount of comma separated strings.

I have a variable (numberOfStrings) which is the number of different strings I need. I just want to loop thru the aryDrivers and assign then to the different strings.

Dim aryHeats(numberOfStrings - 1) As ArrayList

Dim aryDrivers() As String
aryDrivers = txtBatch.Text.Split(",")

For i As Integer = 0 To aryDrivers.Length - 1
    For j As Integer = 0 To aryHeats.Length - 1
        aryHeats(j).Add(aryDrivers(i) & ",")
    Next
Next

For some reason I'm getting an error in the loop when I try to "ADD" the string.

Thoughts?

Thanks!

** Update **

Maybe this will help explain more what I'm trying to do. I have a string:

s = A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z

i'm passing a variable (numberOfHeats) lets use 4.

i would like to then have 4 strings (so I am wanted to use array)

ary(0) = A,E,I,M,Q,U,Y
ary(1) = B,F,J,N,R,V,Z
ary(2) = C,G,K,O,S,W
ary(3) = D,H,L,P,T,X

hopefully that clears this up.

3
  • Do you want to add anyDrivers string array items to anyHeats array list? Commented Apr 17, 2012 at 18:35
  • yes. i'm getting an error. Object is nothing. Commented Apr 17, 2012 at 18:40
  • i was able to figure this out, but it won't let me post and answer until tomorrow. the key was I just needed to not declare it as array or arraylist but as a string. Dim aryHeats(numberOfHeats - 1) As String Commented Apr 17, 2012 at 19:24

2 Answers 2

2

You could use LINQ, although i hate VB.NET method syntax:

Dim text = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
Dim numberOfHeats = 4
Dim aryHeats As String()() = s.Split(","c).
        Select(Function(w, index) New With {.Word = w, .Index = index}).
        GroupBy(Function(x) x.Index Mod numberOfHeats).
        Select(Function(grp) grp.Select(Function(x) x.Word).ToArray()).
        ToArray()

Explanation: it takes the initial string and split it in words (comma as separator). Then it transforms the word and the according index in the String-Array to an anonymous type with Word and Index as properties. This list will be grouped by Index Mod numberOfHeats(the number of arrays you want). This implicitely orders by your desired result. The last step is to transform the groups to a jagged array.

Result:

(0) 
    (0) "A" 
    (1) "E" 
    (2) "I" 
    (3) "M" 
    (4) "Q" 
    (5) "U" 
    (6) "Y" 
(1) 
    (0) "B" 
    (1) "F" 
    (2) "J" 
    (3) "N" 
    (4) "R" 
    (5) "V" 
    (6) "Z" 
(2)     
    (0) "C" 
    (1) "G" 
    (2) "K" 
    (3) "O" 
    (4) "S" 
    (5) "W" 
(3) 
    (0) "D" 
    (1) "H" 
    (2) "L" 
    (3) "P" 
    (4) "T" 
    (5) "X"
0
0

Try this

Dim aryHeats(numberOfStrings - 1) As ArrayList

Dim aryDrivers() As String
aryDrivers = txtBatch.Text.Split(",")

For i As Integer = 0 To aryDrivers.Length - 1
    aryHeats(  i Mod aryHeats.Length  ).Add(aryDrivers(i) & ",")

Next
1
  • okay, i see what you're trying, but that doesn't use my numberOfHeats variable. I updated my post to give a better example of what i'm trying to do. thanks. Commented Apr 17, 2012 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.