Need help displaying output for Golf Game - vb.net

I need some help, I don't know how I would display the players and the hole that they won in this program.
Public Class GolfGame
Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
Dim array(3, 8) As Integer
Dim intLow As Integer = 0
Scores.Items.Add(vbTab & "Hole 1" & vbTab & "Hole 2" & vbTab & "Hole 3" & vbTab & "Hole 4" & vbTab & "Hole 5" & vbTab & "Hole 6" & vbTab & "Hole 7" & vbTab & "Hole 8" & vbTab & "Hole 9")
Randomize()
For outer = 0 To 3
For inner As Integer = 0 To 8
array(outer, inner) = Int((8 * Rnd()) + 1)
Next
Scores.Items.Add("Player " & outer + 1 & vbTab & array(outer, 0) & vbTab & array(outer, 1) & vbTab & array(outer, 2) & vbTab & array(outer, 3) & vbTab & array(outer, 4) & vbTab & array(outer, 5) & vbTab & array(outer, 6) & vbTab & array(outer, 7) & vbTab & array(outer, 8))
Next
For i As Integer = 0 To 8
For j As Integer = 0 To 3
If array(j, i) < intLow Then
intLow = array(j, i)
If j = 0 And i = 0 Then
P1.Text = "Player 1 has won this hole."
ElseIf j = 1 And i = 0 Then
P2.Text = "Player 2 has won this hole."
ElseIf j = 2 And i = 0 Then
P3.Text = "Player 3 has won this hole"
ElseIf j = 3 And i = 0 Then
P4.Text = "Player 4 has won this hole."
End If
End If
Next
intLow = 9
Next
End Sub
End Class
The If statement does not seem to be working within the loop. Any ideas to tackle this? Should the output just be one big if statement, or is there something simpler that I am just missing?

It looks like you're just learning, so I'll try to help you out.
First of all, you don't say how Scores and P1 through P4 are defined. It looks like Scores might be a List(Of String) and P1 through P4 are text boxes or labels of some sort. I'll make those assumptions for now. You begin by sending output to Scores, but then you use the text boxes instead. You need to settle on how you're going to display your output and go with one option.
You begin by assigning a random integer from 1 to 8 for each player/hole combination. That part looks OK.
Then you've attempted to write an algorithm to determine which player won each hole. What you've written is very confused and won't work. There are multiple problems.
intLow is 0 when you attempt to find the winner of the first hole. Then you set it to 9 for holes 2 through 9 (outer 1 through 8). That can't work. You need to be consistent.
When intLow is 0, your main If statement:
If array(j, i) < intLow Then
...will always fail, because the values you've stored in Scores will never be less than zero.
On subsequent iterations, your main If statement will always succeed because intLow has been set to 9 and the maximum score is 8. However, by this time, i will not be 0 and all your inner If statements will fail.
intLow contains the wrong information. You want to store the player who won the hole, not what the low score was. If intLow contains the index of the player who won the hole, then you can easily get the score.
So your program essentially does nothing. You need to adjust the algorithm to properly determine which player won a hole. This would do it:
For i As Integer = 0 To 8
intLow = 0
For j As Integer = 1 To 3
If array(j, i) < array(intLow, i) Then
intLow = j
End If
Next j
' At this point, you know that player intLow+1 won hole i+1 with a score of
' array(intLow, i). Ties always go to the first player who made that score.
Next i
In this revision, notice that we compare players to one another rather than to a constant value like 9. When determining the minimum or maximum in a list, this is a better practice. Granted, the scoring range for a hole of golf is always 1 - 8, but most minimums and maximums tend to have a way of changing over time. You want your algorithm to work no matter what.
I'll leave the output part up to you. This looks like an assignment and I've probably given too much away already.
Other suggestions:
Scores is a poorly named variable. It contains output, so Output might be a better name.
array is not descriptive. It contains scores. Once Scores is renamed, a good name for array would be Scores.
i could be called hole and j could be called player. That would help you keep things straight.
intLow could be called winner, which implies that the variable contains the index of the winner. intLow could mean low score or player with the lowest score. This ambiguity might have confused you. It definitely confused me.
There are native .Net methods for truncating the decimal portion of a number and for generating random numbers. There are even ways to generate random integers directly. You are using a legacy way of doing it, which VB.net provides for backward compatibility with VB6. That's 1990s technology. Programmers unfamiliar with VB6 will look at your code will find this very confusing, which is why it's generally better to use the native features of the .Net framework.

Related

Array out of bounds error Visual basic forms

Hi im new to this but my code keeps giving me the same error after ive entered three countries and their details. Once i click Ok on my third data entry i get an out of bounds error message.
here is my code if anyone can help
Dim countries(2) As employee
Dim row As Integer
For row = 0 To 2
countries(row).CountryName = InputBox("row" & row & " Enter name of country")
countries(row).population = InputBox("row" & row & " Enter population")
countries(row).Capital = InputBox("row" & row & " enter capital city")
countries(row).GDP = InputBox("row" & row & " Enter GDP value ")
countries(row).worldRanking = InputBox("row" & row & " GDP world rank")
countries(row).Democracy = InputBox("row" & row & " Democracy based? (1 = yes 2 = no")
Next row
For row = 0 To 4
If countries(row).worldRanking < 100 Then
ListBox1.Items.Add(countries(row).CountryName)
End If
Next row
You've defined a 3-element array here:
Dim countries(2) As employee
(2) = 0 to 2 = three elements.
So countries(row) where row > 2 isn't defined.
Arrays aren't always the best choice for collections. Have a look at IEnumerable, List, etc which have more flexibility (IMHO).
You could use GetUpperBound() in your loops, instead of hard-coding a number:
For row = 0 To countries.GetUpperBound(0)

VB InputBox Validation Inquiry

I know InputBox isn't the best for validation, but that was one of the specifications on the program I am writing for class. My problem is that despite the if or case statements I make to validate the data entered, it still accepts the data while simultaneously displaying the MsgBox's I have in my code..
Essentially what I would like the case statement to do is to properly filter the data that is entered and not proceed onto the next floor if the data is invalid and request that new data be entered. If the data is valid, proceed onto the next floor.
Const ROOMS As Integer = 30
Const MAX_FLOOR As Integer = 16
Dim floor As Integer
Dim StrOccupancy As String
Dim occupancy As Integer
Dim occupancyRate As Double
Dim occupancySum As Integer
Dim overallRate As Double
lblOccupancyRate.Text = String.Empty
lblRoomsOccupied.Text = String.Empty
output.Items.Clear()
For floor = 1 To MAX_FLOOR
If floor = 13 Then
Continue For
End If
StrOccupancy = Integer.TryParse(InputBox("Enter the number of rooms occupied for floor:" & Space(1) & floor), occupancy)
Select Case occupancy
Case < 1
MsgBox("Please enter a number of 1 or more occupants.")
Case > 30
MsgBox("Amount of occupants must be between 1-30.")
Case >= 1 And occupancy <= 30
occupancyRate = (occupancy / ROOMS)
occupancySum += occupancy
overallRate = occupancySum / (ROOMS * 15)
End Select
output.Items.Add("Floor: " & floor & " Rooms Occupied: " & occupancy _
& " Occupancy Rate: " & occupancyRate.ToString("P2"))
lblRoomsOccupied.Text = occupancySum.ToString
lblOccupancyRate.Text = overallRate.ToString("P2")
Next
output.Items.Add("")
output.Items.Add("Total occupancy is" & Space(1) & occupancySum & Space(1) & "and" & Space(1) & overallRate.ToString("P2") & Space(1) & " of rooms are full.")
End Sub
Had a little time to check your code and actually you need a Boolean that validates if the occupancy requirements are met and loop if not. The code would be something like this:
For floor = 1 To MAX_FLOOR
'Boolean to validate the occupancy meet the requirements
Dim goodOccupancy As Boolean = False
'Do loop enters at least 1 time and runs the code inside it
Do
Integer.TryParse(InputBox("Enter the number of rooms occupied for floor:" & Space(1) & floor), occupancy)
Select Case occupancy
Case < 1
MsgBox("Please enter a number of 1 or more occupants.")
Case > 30
MsgBox("Amount of occupants must be between 1-30.")
Case >= 1 And occupancy <= 30
occupancyRate = (occupancy / ROOMS)
occupancySum += occupancy
overallRate = occupancySum / (ROOMS * 15)
'If the requirements are met we change the Boolean value to continue with the execution
goodOccupancy = True
End Select
'We loop if the requirements are not met
Loop Until goodOccupancy = True
output.Items.Add("Floor: " & floor & " Rooms Occupied: " & occupancy _
& " Occupancy Rate: " & occupancyRate.ToString("P2"))
lblRoomsOccupied.Text = occupancySum.ToString
lblOccupancyRate.Text = overallRate.ToString("P2")
Next
But please next time try to be more explicit with your code. Most of people won't check what is going on. Do not expect people to solve your problems without guidance. Please read the How to ask guide it will give you a light to use the site as it is supposed to be used

Reading ListView from BackgroundWorker

I've tried doing some searchs but for the life of me I don't seem to be able to find the answer, or a suggested solution that works. Its probably my understanding, but hopefully asking my own question will give me an answer that works :o)
I have a Windows Form Application which consists of one item, ListView1
This ListView has items added to it from a file via a Drag / Drop which is done on the main UI Thread, no background worker, it consists of around 1500 rows.
I'm trying to get a background worker now to read this ListView, but I'm getting a Cross Threading error as ListView1 was not created on the same thread.
The error comes on the simplest of pieces of code, but I don't seem to be able to think of a way around it or implementing an invoke etc.
For i = 0 To Me.ListView1.Items.Count - 1
ValueStatement = ValueStatement & "(" & Me.ListView1.Items(i).SubItems(0).Text
If i = Me.ListView1.Items.Count - 1 Or counter = 500 Then
CommaTerminate = ";"
Else
CommaTerminate = ","
End If
For y = 0 To Me.ListView1.Columns.Count - 1
ValueStatement = ValueStatement & "'" & Me.ListView1.Items(i).SubItems(y).Text & "'"
If y = Me.ListView1.Columns.Count - 1 Then
ValueStatement = ValueStatement & ")"
Else
ValueStatement = ValueStatement & ","
End If
Next
ValueStatement = ValueStatement & CommaTerminate & vbNewLine
If counter = 500 Then
SQLStatement = "INSERT INTO RAW_CLI_DATA_" & GlobalVariables.CDR_Company & " VALUES " & vbNewLine & ValueStatement
GenericDatabaseRequest(SQLStatement, "Loading RAW table with data..")
counter = 0
ValueStatement = ""
End If
counter = counter + 1
Next
The error comes on the line ValueStatement = ValueStatement & "(" & Me.ListView1.Items(i).SubItems(0).Text
Thanks for any help!
It sounds like you went down the wrong road early on. The ListView is supremely illsuited for database ops:
Everything is contained as String which means somewhere you will have code to convert it to other types
It does not support databinding which means you have to manually create rows...
... then later iterate them to fish the data back out.
A DataGridView and DataTable would be simpler: When the user enters data into the control and it would be stored in the table and as the proper type. Setting the DataTable as the Datasource, the DataGridView would create the display (rows and columns) for you.
Some of the time it takes will be consumed by SQLite to perform the INSERT, but it also looks like you are spending a lot of time iterating and concatenating SQL. It's usually better to work with the data than the user's View of it anyway, so extract and pass the data to the worker.
First, suck the data out of the ListView into a String()() container:
Dim data = lv.Items.
Cast(Of ListViewItem).
Select(Function(s) New String() {s.SubItems(0).Text,
s.SubItems(1).Text,
s.SubItems(2).Text}).
ToArray()
Then pass it to the BackGroundWorker:
bgw.RunWorkerAsync(data)
The DoWork Event:
Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork
' unbox the data
Dim dataToInsert = CType(e.Argument, String()())
For n As Int32 = 0 To 2
Console.WriteLine("[{0}], [{1}], [{2}]", dataToInsert(n)(0),
dataToInsert(n)(1),
dataToInsert(n)(2))
Next
End Sub
Results:
[Patient Tempest], [Lorem ipsum dolor sit], [Swordfish]
[Sour Priestess], [hendrerit nibh tempor], [Perch]
[Frozen Justice], [Interdum ex felis], [Swordfish]
It correctly prints the random data I put into the LV.
This will allow you to process the ListView Data in the BackGroundWorker but it wont really save any time, it just keeps the UI unlocked. The real problem is elsewhere, probably in the DB ops.

Excel VBA: Looking for Advice Avoiding an Infinite Loop

Imgur Album with screens of worksheets: http://imgur.com/a/6rFWF
Long story short, I am writing an Excel VBA utility that will assign two types of security shifts (called coverages and weekend duties) to security staff members. Basically, I have a worksheet with all of the staff members and their various availability information in it (the top image in the imgur album) and a worksheet with all of the coverage dates in it (the bottom image in the imgur album). Note that I don't have an image of the weekend duty dates as it looks similar to the coverage dates (but with the Friday and Saturday shifts).
The utility basically assigns a random staff member to each date, checking to make sure it doesn't violate any of their availability requirements. Unfortunately, I realize that I am creating a large chance for an infinite loop to occur. In my own testing, there has only been 1 attempt out of around 15-16 that did not enter an infinite loop near the end. So I'm looking for your help to account for this so the utility doesn't eat itself.
Here is the "pseudo-code" for the procedure in question.
'Loop for Column A in the Coverage Slips sheet (image 2 in imgur album)
Do Until (CoverageRowNumber = LastCoverageSlipRow + 1)
Get a Random Staff Member by RNG
If staff member still needs more shifts (see Requirements columns) Then
If staff member does not have an "X" under the day of the week Then
If staff member does not have a matching date conflict Then
Assign the coverage
Increase CoverageRowNumber
End If
End If
End If
Loop
'Loop for Column B in the coverage slips sheet (image 2 in imgur album)
Do Until...
Same as the loop above
Loop
Edit: Disregard that I have the dates in two columns for now. I'll be fixing that once I solve the problem of this post...it's an easy fix and will cut the code almost in half.
The problem is that as the utility gets near the end of the list of dates, it often runs into the scenario where the only staff members left cannot sit that specific shift (whether because of day of the week or specific date). In the event that it runs into this scenario, I can see a couple of acceptable options (though I don't know how I'd go about programming them):
Undo all of the work that the utility did and start over until it can get lucky and find a solution that works. This would save me some time doing manual placements for the last few shifts but might take a very long time. Additionally, I'd have to store all of the original values and then paste them back into the spreadsheet anytime it starts over.
Simply stop assigning shifts and just exit the procedure. I will be able to manually place the last few shifts by moving a few people around. I sure is a lot less work than manually assigning 200 shifts by hand like I've been doing it the past few years.
Do you guys have any thoughts that could be of help here? I'm not even sure how I could have the procedure check to see if there are any available options or not, but either way there's got to be a way to detect (and deter) this infinite loop before it crashes the program.
Sorry for the novel, and thanks in advance for any help!
Edit: In an effort to provide a little more clarity, I figured I'd copy and paste the actual code below:
'------------------------------------------------------------'
'Create ws variables for each worksheet
Dim wsConflicts As Worksheet
Dim wsCoverageSlips As Worksheet
Dim wsWDSlips As Worksheet
Dim wsCoverageOutput As Worksheet
Dim wsWDOutput As Worksheet
'------------------------------------------------------------'
Public Function SetSheets()
'Assign the worksheets to the ws variables
Set wsConflicts = Worksheets("Conflicts")
Set wsCoverageSlips = Worksheets("Coverage Slips")
Set wsWDSlips = Worksheets("WD Slips")
Set wsCoverageOutput = Worksheets("Coverage Output")
Set wsWDOutput = Worksheets("WD Output")
'Display a message (debugging)
'MsgBox "The sheets have been assigned successfully"
End Function
'------------------------------------------------------------'
Public Function ColumnLetter(ColumnNumber As Integer) As String
Dim n As Long
Dim c As Byte
Dim s As String
n = ColumnNumber
Do
c = ((n - 1) Mod 26)
s = Chr(c + 65) & s
n = (n - c) \ 26
Loop While n > 0
ColumnLetter = s
End Function
'------------------------------------------------------------'
Sub AssignCoverages()
'Fill the ws variables
Call SetSheets
'Set the first and last row numbers
Dim FirstStaffMemberRow As Integer
FirstStaffMemberRow = 3
Dim LastStaffMemberRow As Integer
LastStaffMemberRow = wsConflicts.UsedRange.Rows.Count
'Count the number of required coverages and weekend duties
Dim RequiredCoverages As Integer
Dim RequiredWDs As Integer
For i = FirstStaffMemberRow To LastStaffMemberRow
RequiredCoverages = RequiredCoverages + wsConflicts.Range("B" & i).Value
RequiredWDs = RequiredWDs + wsConflicts.Range("C" & i).Value
Next i
'Display a message (debugging)
MsgBox "You currently have " & RequiredCoverages & " required coverages and " & RequiredWDs & " required weekend duties."
'Count the number of coverage slips and weekend duty slips
Dim FirstCoverageSlipRow As Integer
FirstCoverageSlipRow = 1
Dim LastCoverageSlipRow As Integer
LastCoverageSlipRow = wsCoverageSlips.UsedRange.Rows.Count
Dim NumCoverageSlips As Integer
NumCoverageSlips = (LastCoverageSlipRow - FirstCoverageSlipRow + 1)
Dim FirstWDSlipRow As Integer
FirstWDSlipRow = 1
Dim LastWDSlipRow As Integer
LastWDSlipRow = wsWDSlips.UsedRange.Rows.Count
Dim NumWDSlips As Integer
NumWDSlips = (LastWDSlipRow - FirstWDSlipRow + 1)
'Check to make sure there are enough required shifts for slips
If RequiredCoverages <> NumCoverageSlips Then
MsgBox "The number of shifts you require (Columns B & C on Conflicts sheet) does not match the number of slips you've entered. You have " & RequiredCoverages & " required coverages and " & NumCoverageSlips & " coverage slips. You have " & RequiredWDs & " required weekend duties and " & NumWDSlips & " weekend duty slips. Please correct this error and retry."
Exit Sub
Else
'Debugging
'MsgBox "The number of shifts you require (Columns B & C on Conflicts sheet) matches the number of slips you've entered. You have " & RequiredCoverages & " required coverages and " & NumCoverageSlips & " coverage slips. You have " & RequiredWDs & " required weekend duties and " & NumWDSlips & " weekend duty slips."
End If
'Massive loop to assign coverages to random staff members
Dim NumRemainingCoverages As Integer
NumRemainingCoverages = NumCoverageSlips
Dim SlipRowNumber As Integer
SlipRowNumber = FirstCoverageSlipRow
'Loop for Column A
Do Until (SlipRowNumber = LastCoverageSlipRow + 1)
'Get a random staff member row
StaffMemberRow = GetRandomStaffMemberRow(FirstStaffMemberRow, LastStaffMemberRow)
'Check to make sure the staff member has remaining required coverages
If wsConflicts.Range("B" & StaffMemberRow).Value > 0 Then
'Check to make sure the staff member can sit the day of the week
Dim CurrentDate As Date
CurrentDate = wsCoverageSlips.Range("A" & SlipRowNumber).Value
Dim CurrentDay As Integer
CurrentDay = Weekday(CurrentDate)
Dim CurrentDayColumn As String
If CurrentDay = 1 Then CurrentDayColumn = "D"
If CurrentDay = 2 Then CurrentDayColumn = "E"
If CurrentDay = 3 Then CurrentDayColumn = "F"
If CurrentDay = 4 Then CurrentDayColumn = "G"
If CurrentDay = 5 Then CurrentDayColumn = "H"
If CurrentDay = 6 Then CurrentDayColumn = "I"
If CurrentDay = 7 Then CurrentDayColumn = "J"
If wsConflicts.Range(CurrentDayColumn & StaffMemberRow).Value = "" Then
'Check to make sure the staff member does not have a date conflict
Dim ColumnNumber As Integer
Dim ColumnLetterText As String
Dim CoverageDateConflicts As Integer
CoverageDateConflicts = 0
For ColumnNumber = 11 To 20
ColumnLetterText = ColumnLetter(ColumnNumber)
Dim CoverageSlipDate As Date
If IsDate(wsConflicts.Range(ColumnLetterText & StaffMemberRow).Value) = True Then
CoverageSlipDate = wsConflicts.Range(ColumnLetterText & StaffMemberRow).Value
Else
CoverageSlipDate = DateValue("01/01/1900")
End If
If CurrentDate = CoverageSlipDate Then
CoverageDateConflicts = CoverageDateConflicts + 1
End If
Next ColumnNumber
If CoverageDateConflicts = 0 Then
'Assign the coverage
Dim BlankCoverageOutputRow As Integer
BlankCoverageOutputRow = wsCoverageOutput.UsedRange.Rows.Count + 1
wsCoverageOutput.Range("A" & BlankCoverageOutputRow).Value = wsConflicts.Range("A" & StaffMemberRow).Value
wsCoverageOutput.Range("B" & BlankCoverageOutputRow).Value = CurrentDate
'Reduce the staff member's required coverages by 1
Dim CurrentRequirements As Integer
CurrentRequirements = wsConflicts.Range("B" & StaffMemberRow).Value
wsConflicts.Range("B" & StaffMemberRow).Value = CurrentRequirements - 1
'Reduce the number of remaning coverages by 1
NumRemainingCoverages = NumRemainingCoverages - 1
'Increase the slip row number by 1
SlipRowNumber = SlipRowNumber + 1
'Message box for debugging
'MsgBox "Coverage Date (" & CurrentDate & ") assigned to " & wsConflicts.Range("A" & StaffMemberRow).Value & "."
End If 'End date check
End If 'End day check
End If 'End requirements check
Loop 'End loop for column A
End Sub
'------------------------------------------------------------'
Public Function GetRandomStaffMemberRow(FirstStaffMemberRow As Integer, LastStaffMemberRow As Integer)
'Pick a random number between the first staff member row and the last
Call Randomize
GetRandomStaffMemberRow = Int((LastStaffMemberRow - FirstStaffMemberRow + 1) * Rnd + FirstStaffMemberRow)
End Function
The question is too open for a detailed answer, so I try with some guidelines. I hope it helps.
I would use a class Solution with the following members:
Solution.ReadInputFromSheet() reads the table from the sheet into the class members
Solution.GenerateRandom() creates a new random solution. Try to find a balance between smart (add some logic to avoid totally random solutions) and speed (don't get stuck, exit after trying 10 or 50 random numbers that don't work), but speed is more important
Solution.Quality() As Double calculates the quality of the solution. For example a solution that is not valid returns 0, if Joe has 10 consecutive shifts returns 20, if the shifts are better distributed returns 100.
Solution.WriteOnSheet() write the data from the class members into the sheet.
Solution.Clone() As Solution() creates a new Solution instance with the same data
Make a cycle that creates a solution, checks if its quality is better than the best quality solution found so far, if it is better keep it, otherwise go and calculate another solution.
Set BestS = New Solution
BestS.ReadInputFromSheet
BestS.GenerateRandom()
Set S = New Solution
S.ReadInputFromSheet
For I = 1 To 10000
S.GenerateRandom()
If S.Quality() > BestS.Quality() Then Set BestS = S.Clone()
Next I
BestS.WriteOnSheet
Instead of 10000 you can use Timer to run it for a finite number of seconds, or make a button to interrupt it when you come back from lunch break.
A faster solution generator function is better than risking of getting stuck with one difficult (or impossible) solution.
For a smarter solution generator function I need more details on the rules.
So I went ahead and developed my own solution to this problem--it's not perfect and it's probably not the best way to handle the scenario. But it works, and it solved my problem in a matter of minutes instead of hours learning other methods.
Basically, I created two new "counter" variables. The first is FailedAttempts. Every time the procedure tries a random staff member but runs into a conflict, it increments FailedAttempts by 1. Every time the random staff member is a successful match (no conflicts), it resets FailedAttempts to 0. If at any time FailedAttempts = 100, it immediately exits the loop and starts over. In other words, if it tries 100 random staff members in a row without finding a match, I assume it's not going to find a match and just cut my losses.
The second variable, Assignments, is incremented by 1 every time that the procedure makes a successful assignment. When this number equals the number of shifts that the procedure is supposed to assign, it immediately exits the loop.
To do this, I had to use a couple of forbidden 'GoTo' commands (I wasn't sure how else to exit the loop. You can exit a For loop with Exit For but I believe this is invalid for Do While loops. I ended up only needing two GoTo's, one for exiting the loop and one to go back to the beginning of the procedure. I also made sure that the cells in the worksheet that change during the procedure are reset to their original state before it retries the assignment procedure.
I'll save everyone the trouble of reading through the extended version of the code, but in 'pseudo-code' form it looks like this:
Retry: 'Label for GoTo command
Do Until (CoverageRowNumber = LastCoverageSlipRow + 1)
Get a Random Staff Member by RNG
If staff member still needs more shifts (see Requirements columns) Then
If staff member does not have an "X" under the day of the week Then
If staff member does not have a matching date conflict Then
'Assign the coverage
'Increase CoverageRowNumber
Assignments = Assignments + 1
Else
FailedAttempts = FailedAttempts + 1
End If
Else
FailedAttempts = FailedAttempts + 1
End If
Else
FailedAttempts = FailedAttempts + 1
End If
If FailedAttempts > 100 Then
GoTo ExitLoop
End If
Loop
ExitLoop: 'Label for GoTo command
If Assignments <> NumCoverageSlips Then
GoTo Retry
End If
'Do rest of procedure
Again, there may be (and certainly is) a more elegant and "correct" way of accomplishing the task at hand. This method worked for me with the given environment. Thanks to those who provided solutions--even though I ended up going a different direction they provided great food for thought and helped me learn a bunch of new methods (especially the class idea from #stenci).
Thanks all.

Double For Loop in VB.NET

Dim ssi(11) As String
For i = 0 To 10
If ssi(i) = "" Then ssi(i) = "0"
For j = 0 To Val(ssi(11)) + i
ssi(i) = xuh(Val(ssi(i)))
Next
Next
If ssi(11) = "2" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10)))
ElseIf ssi(11) = "3" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10))) * (-1)
End If
I am new here and new to VB as well.
I am trying to understand this double loop in vb code.
ssi(i) is defined as a String variable. and each element is assigned to a specific number in a String. Hope I told it clearly.
My problem with this loop is below.
Since i ranges from 0 to 10, what does this j mean? Does j mean the new ssi(1-10) or another whatever number?
I think the best way to answer your question about understanding a double loop is to try looking at something simpler.
The first program I always write in each new version of BASIC that comes along is a 12 times table.
I've modified it a bit below to be a 12 x 10 table for the purpose of illustrating for you how a double loop works ... hope it helps:
For x As Integer = 1 To 12
For y As Integer = 1 To 10
Console.Write(x * y)
Console.Write(vbTab)
Next
Console.WriteLine()
Next