Efficiency question

J

Jeff

Göran A mentioned efficient use of resources in another thread. I'm still
new and know enough to make the code below work, but not enough to do it
well. There has to be a better way.

I'm creating a "progress indicator" that shows as increasingly more cells of
a table row becoming blue as a user goes further
through pages in a site. Because there are various ways to proceed through
the site, I do need to keep the dynamic part about calculating
the % complete as shown on the first line. The rest can be altered.

Could someone suggest a better method that wouldn't involve so many
calculations?

Thanks


Dim PercentageComplete As Integer = (AmountSoFar/ TotalAmount) * 100
Dim X As Integer

If PercentageComplete < 7 Then
X = 0
ElseIf PercentageComplete < 14 Then
X = 1
ElseIf PercentageComplete < 20 Then
X = 2
ElseIf PercentageComplete < 26 Then
X = 3
ElseIf PercentageComplete < 33 Then
X = 4
ElseIf PercentageComplete < 40 Then
X = 5
ElseIf PercentageComplete < 46 Then
X = 6
ElseIf PercentageComplete < 52 Then
X = 7
ElseIf PercentageComplete < 58 Then
X = 8
ElseIf PercentageComplete < 64 Then
X = 9
ElseIf PercentageComplete < 70 Then
X = 10
ElseIf PercentageComplete < 77 Then
X = 11
ElseIf PercentageComplete < 94 Then
X = 12
ElseIf PercentageComplete < 100 Then
X = 13
ElseIf PercentageComplete < 110 Then
X = 14
End If

Dim tr As New TableRow
For j As Integer = 1 To 15
Dim td As New TableCell
td.Height = 5
td.Width = 5
tr.Cells.Add(td)
Table2.Rows.Add(tr)
Next

For i As Integer = 0 To X
Table2.Rows(0).Cells(i).BackColor = Drawing.Color.Blue
Next
 
S

Stephany Young

One way to do it is:

Declare an array with class scope and populate it when the class is
instantiated:

Private m_slots As Integer() = Nothing

Public Sub New()

m_slots = New Integer(100) {}

For _i as integer = 0 to 100
Select Case _i
Case < 7
m_slots(_i) = 0
...
Case < 100
m_slots(_i) = 13
Case Else
m_slots = 14
End Case

End Sub

In your Function/sub (whatever):

Dim PercentageComplete As Integer = (AmountSoFar/ TotalAmount) * 100

For _i As Integer = 0 To m_slots(PercentageComplete)
Table2.Rows(0).Cells(_i).BackColor = Drawing.Color.Blue
Next

If you had the number of columns being an exact divisor of 100 then it would
be even easier.

For example, for 20 columns

For _i As Integer = 0 To PercentageComplete \ 5
Table2.Rows(0).Cells(_i).BackColor = Drawing.Color.Blue
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top