Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 字符串变量的for循环_Vb.net_String_For Loop - Fatal编程技术网

Vb.net 字符串变量的for循环

Vb.net 字符串变量的for循环,vb.net,string,for-loop,Vb.net,String,For Loop,这是我的密码- for i as integer = 0 to rows.count - 1 output &= "Name =" & row(i)("Name") output &= "lastName =" & row(i)("lastName") ... 50 more fields next 我需要这样的输出 应用程序1Name=MikeApplicant1lastName=ditkaApplicant2Name=TomApplicant2l

这是我的密码-

for i as integer = 0 to rows.count - 1
   output &= "Name =" & row(i)("Name")
   output &= "lastName =" & row(i)("lastName")
... 50 more fields
next
我需要这样的输出

应用程序1Name=MikeApplicant1lastName=ditkaApplicant2Name=TomApplicant2lastName=Brady

我如何做到这一点,而不把下面的代码50次- 输出&=“申请人”和i.tostring()+1&“名称=”行(i)(“名称”) ... 等等 有没有办法做一个for循环并运行申请人1,2,3,4。。。。一枪?
谢谢

您真的无法添加50个不同的字段。 唯一可以缩短的是变量名:

Dim strLN as String = row(i)("lastName")
Dim strFirstName as String = row(i)("firstName")
然后你就把它们放在一起

output &= strLN & strFirstName...etc

看起来您希望创建一个包含所有字段的数组,然后包含一个嵌套循环

    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
尝试:

Dim输出为新的StringBuilder(“”)
对于i,整数=0到行。计数-1
output.append(“申请人”+i.ToString())
Foreach(col作为dt.Columns中的DataColumn)'行所在的数据表
Dim colName as string=col.ColumnName
output.append(colName&“=”&行(i)(colName.ToString())
下一个
如果i

StringBuilder的字符串连接速度更快,如果您将行保存在数据表中(我假设这样做是因为您访问这些行时看起来是这样的),那么您可以在顶层遍历列名。

哪里是“申请者”部分?我需要应用程序1的名称,应用程序2的名称等,大约50多个领域,这是不必要的。他可以循环浏览已经存在的列。在我看来,这比单独列出所有字段要好。没错,我没有想到这一点。我大脑中存储数据集和数据表信息的部分由于ORM的过度使用而萎缩了。正确答案是+1,如果我提到使用StringBuilder,我会给你+2。很难夸大仅使用字符串连接大量文本对性能的影响。line-Foreach(col作为dt.Columns中的DataColumn)在哪里声明col?@iregy-col在该行中声明为DataColumn。通过在()中执行此操作,可以将其作用域保持在立即数块中。它会给我一个错误,告诉我“名称'col'未声明”这是我编写的代码-使用ds.Tables(2)为i设置整数=0到.Rows.Count-1 output.Append(col作为ds.Tables(2)中的DataColumn))output.Append(“GivenName”&.Rows(i)(“firstname”)和vbCrLf)output.Append(“LastName”&.Rows(i)(“LastName”)和.Rows(i)(“LastName”)&vbCrLf)Next如果i<.Rows.Count-1,则输出.Append(“| |”)下一个结束With@iregy:假设您使用了“With”关键字,您需要在for each this way-->for each中声明col(col as DataColumn in.Columns)
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