views:

134

answers:

3

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

A: 

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
JonH
where is the "Applicant" part? i need Applicant1Name,Applicant2Nameand so on for like 50 more fields
iregy
A: 

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
NerdFury
That is unnecessary. He can loop through the already existing columns.Still better in my opinion than having to list all the fields individually.
Helgi Hrafn Gunnarsson
True, didn't think about that. The part of my brain that stores information about DataSets and DataTables has atrophied from excessive ORM use.
NerdFury
+5  A: 

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.

Joel Etherton
+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.
Helgi Hrafn Gunnarsson
+1 for StringBuilder, it's AMAZING the difference it makes
Chad
the line - Foreach(col as DataColumn in dt.Columns), where do i declare col?
iregy
@iregy - col is declared in that line as a DataColumn. By doing it inside the () there, you keep its scope to the immediate block.
Joel Etherton
iregy
@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)
Joel Etherton