2

this is my code -

for i as integer = 0 to rows.count - 1
   output &= "Name =" & row(i)("Name")
   output &= "lastName =" & row(i)("lastName")
... 50 more fields
next

i need the output to be like this

Applicant1Name = MikeApplicant1lastName = ditkaApplicant2Name = TomApplicant2lastName = Brady ...

how do i do this without putting the following code 50 times - output &= "Applicant" & i.tostring() + 1 &"Name =" & row(i)("Name") ... and so on. is there a way to make a for loop and run applicant 1,2,3,4.... in one shot? thanks

3 Answers 3

6

Try:

Dim output as New StringBuilder("")

For i as Integer = 0 To rows.Count - 1
    output.append("Applicant" + i.ToString())
    Foreach(col as DataColumn in dt.Columns)  ' The datatable where your rows are
        Dim colName as string = col.ColumnName
        output.append(colName & "=" & rows(i)(colName).ToString())
    Next
    If i < rows.Count - 1 Then output.Append("|")
Next

StringBuilder is faster for string concatenations, and if you keep your rows in a datatable (which I assume is happening because that's how it looks like you're accessing them), then you can just iterate through the columnnames at the top level.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 for the correct answer, and I'd give you +2 if I could for mentioning using StringBuilder. It is hard to exaggerate the performance impact of concatenating lots of text with the use of strings only.
the line - Foreach(col as DataColumn in dt.Columns), where do i declare col?
@iregy - col is declared in that line as a DataColumn. By doing it inside the () there, you keep its scope to the immediate block.
it gives me an error saying "Name 'col' is not declared" this is the code i made - With ds.Tables(2) For i As Integer = 0 To .Rows.Count - 1 output.Append("Applicant") For Each (col As DataColumn In ds.Tables(2)) output.Append("GivenName||" & .Rows(i)("firstname") & vbCrLf) output.Append("LastName||" & .Rows(i)("lastname") & vbCrLf) Next If i < .Rows.Count - 1 Then output.Append("||") Next End With
@iregy: Given that you've used the "With" keyword, you'll need to declare col in the for each this way --> For Each (col as DataColumn In .Columns)
0

You really cant as you are trying to append 50 different fields. The only thing you can shorten is the variable name:

Dim strLN as String = row(i)("lastName")
Dim strFirstName as String = row(i)("firstName")

Then you simply put it all together

output &= strLN & strFirstName...etc

1 Comment

where is the "Applicant" part? i need Applicant1Name,Applicant2Name and so on for like 50 more fields
0

looks like you want to create an array of all the fields you have and then include a nested loop.

    Dim fields As String() = {"Name", "LastName", "SSN", "Birthdate"}
    Dim output As String = ""

    For i As Integer = 1 To rows.count
        For Each field As String In fields
            output = String.Concat(output, "Applicant ", i, field, "=", row(i)(field), " ")
        Next
    Next

2 Comments

That is unnecessary. He can loop through the already existing columns. Still better in my opinion than having to list all the fields individually.
True, didn't think about that. The part of my brain that stores information about DataSets and DataTables has atrophied from excessive ORM use.

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.