|
-
Oct 28th, 2009, 03:46 PM
#1
Thread Starter
Lively Member
Need help w/ loop w/in loop???
I had an earlier thread but it turns out the problem is more complicated than initially expected. This is a new updated thread.
I'm creating a vb program that uses rows from a dataset to create a text output.
However, I think I need a loop w/in a loop b/c some users have multiple rows but I want it contained w/in one record of the output.
Hope that makes sense....here's what I have so far:
Code:
For each row in ds.tables("1234").rows
A1 = String.Format("{0,5}..etc", employeeID, address etc.)
sw.writeline (A1)
A2 = String.Format("{0,5}..etc", Option, address etc.)
sw.writeline (A2)
A3 = String.Format("{0,5}..etc", dependent, address etc.)
sw.writeline (A3)
Next
Now, say that employees have more than one "Option" and can choose one or the other OR both. The way it's set up now, it will output the employee's info a second time if the employee has both options selected. Like so
Employee A
Option 1
dependent
Employee A
Option 2
dependent
Employee B
Option 1
dependent
I'm trying to get the output to look like the following (assuming first employee selects both options and the next employee only selected one):
Employee A
Option 1
Option 2
dependent
Employee B
Option 1
dependent
I was thinking I could maybe use an "if" statement but can't come up with the proper logic. I'd like to say, "If Employee Info has already been output, jump to A2"
Any help is appreciated. Let me know if my explaination is unclear and I'll do my best to clarify.
Thanks.
-
Oct 28th, 2009, 04:07 PM
#2
Re: Need help w/ loop w/in loop???
outside the loop declare a variable to hold the employee id... set it to some default value... say, -1 for instance.
Inside the loop.... If the employee id variable does NOT match the employeeID of the record, output the employee part of the record and store the employeeID from the record in the variable.
proceed to output your options....
the dependant part of the record though..... hmmmmm....
-tg
-
Oct 28th, 2009, 04:17 PM
#3
Thread Starter
Lively Member
Re: Need help w/ loop w/in loop???
Yeah, that's my big hangup. My initial thread only had "A1" and "A2" which would work w/ your solution. However, adding the "A3" portion is making it more complicated.
-
Oct 28th, 2009, 04:25 PM
#4
Re: Need help w/ loop w/in loop???
I got it.... see if this works for you:
Code:
dim EmplID as Integer = -1
dim A3 as String = ""
For each row in ds.tables("1234").rows
'Assumption: employeeID is a field from the row somewhere....
If EmplID <> employeeID then
If A3.Length > 0 Then
sw.writeline(A3) 'Write the dependant from the previous row...
End If
'write out the employee
sw.writeline (String.Format("{0,5}..etc", employeeID, address etc.))
'build the dependant string
A3 = String.Format("{0,5}..etc", dependent, address etc.)
End If
'Write the option
sw.writeline (String.Format("{0,5}..etc", Option, address etc.))
Next
sw.writeline(A3) 'Write the dependant from the last row
it's a little futzy... and could probably be cleaned up some....
-tg
-
Oct 28th, 2009, 04:28 PM
#5
Re: Need help w/ loop w/in loop???
What about something like this?
vb.net Code:
Public Sub Method() Dim table As New DataTable("Employees") With table With .Columns .Add("EmployeeID", GetType(Integer)) .Add("EmployeeName", GetType(String)) .Add("OptionName", GetType(String)) End With With .Rows .Add(1, "David", "Option 1") .Add(1, "David", "Option 2") .Add(2, "Jack", "Option 1") .Add(3, "Amy", "Option 1") .Add(4, "Mark", "Option 1") .Add(4, "Mark", "Option 2") End With End With Dim results = From row In table.AsEnumerable() _ Group By employeeId = CInt(row.Item("EmployeeID")) Into groups = AsEnumerable() _ Select New With {.EmployeeID = employeeId, _ .EmployeeName = groups.Where(Function(row) CInt(row("EmployeeID")) = employeeId) _ .Select(Function(row) row("EmployeeName").ToString()).FirstOrDefault(), _ .Options = groups.Where(Function(row) CInt(row("EmployeeID")) = employeeId) _ .Select(Function(row) row("OptionName").ToString()).ToArray()} For Each element In results MessageBox.Show(String.Format("{0}{1}{2}", element.EmployeeName, _ Environment.NewLine, String.Join(Environment.NewLine, element.Options))) Next End Sub
I assumed a table structure...
-
Oct 28th, 2009, 04:31 PM
#6
Addicted Member
Re: Need help w/ loop w/in loop???
One solution might be to first declare a counter outside the loop to keep track on wich row the loop is at. So for each iteration in the loop, increase the value by one.
Now, make a new loop inside your existing loop like this (if your integer is declared as 'counter':
vb.net Code:
For x As Integer = counter To ds.tables("1234").rows.Count Next
In this loop you will have a If-statement, wich will replace your A2 = part.If the row is equal to A1, print out the A2 values. else, exit the loop. So the loop will look something like this:
vb.net Code:
For x As Integer = counter To ds.tables("1234").rows.Count If String.Format("{0,5}..etc", employeeID, address etc.) = A1 Then A2 = String.Format("{0,5}..etc", Option, address etc.) sw.writeline (A2) Else Exit For End If Next
A bit messy, but I guess you'll get my idea
-
Nov 6th, 2009, 04:40 PM
#7
Thread Starter
Lively Member
Re: Need help w/ loop w/in loop???
I think I found a solution based on some information I gathered from the other posts.
I decided to use a seperate data set of just the employee ID and Option. So now, when it gets to the option line, I start a new loop that checks to see if the EmplID matches the emply id from the first line, if it does, it will contiue to write the option line until it reaches an EmplID that does not match the orignial one taken from the first dataset.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|