user6000503
user6000503

Reputation:

VB.Net Break Foreach Loop

How make an output like this:

English James Moore, Math Karen Hatskin, Science Quennie Orph (using foreach loop ?)

i have this code -

dim T.Items.Add("James Morre","Karen Hatskin","Quennie Orph")
dim S.Items.Add("English","Math","Science")

For Each teacherItem as string In T.Items
  For Each subjectItem as string In S.Items

  Next
Next

Update:

  For Each teacherItem As String In tSub.Items
                Try
                    For Each subjectItem As String In avSub.Items
                        Using cmd As New SqlClient.SqlCommand("insert into Student_Grade values('" & txt_lrn2.Text & "','" & txt_name.Text & "','" & txtgrade.Text & "','" & txtsection.Text & "','" & subjectItem.ToString & "','" & teacherItem.ToString & "',0,0,0,0,0,'" & SY.Text & "')", cn)
                            x = cmd.ExecuteNonQuery
                        End Using
                    Next
                Catch ex As Exception
                    Continue For
                End Try
            Next

but my output below is so redundant . .

please click here for image

Upvotes: 0

Views: 1526

Answers (1)

Enigmativity
Enigmativity

Reputation: 117027

Try this:

Dim T = {"James Morre", "Karen Hatskin", "Quennie Orph"}
Dim S = {"English", "Math", "Science"}

Dim n = Math.Min(T.Length, S.Length)

For i = 0 To n - 1
    Console.Write(S(i) & " " & T(i))
    If i < n - 1 Then
        Console.Write(", ")
    End If
Next

That gives:

English James Morre, Math Karen Hatskin, Science Quennie Orph

Alternatively, without using any loops, this works too:

Console.WriteLine(String.Join(", ", S.Zip(T, Function (x, y) x & " " & y)))

Upvotes: 1

Related Questions