Reputation: 15
I know this question maybe super easy to some so please don't eat my head for asking, but I have been struggling to figure this out:
I will like to use the condition from IF Else statement from a different method to redirect user to different pages based on if they pass or fail. this is what my code looks like:
Public passScore As Boolean
Public Sub BindGrid()
.....
If calcTotal >= 70 Then
passScore = True
btnNext.Visible = True
Else
passScore = False
btnNext.Visible = False
End If
End Sub
this is where I am struggling.. being able to use the result from the calculation to redirect user after the click a button:
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
If passScore = True Then
Response.Redirect(mstrPassSlide, False)
else
Response.Redirect(mstrFailSlide, False)
End If
Catch ex As Exception
Throw ex
Finally
End Sub
New Code Changes
I tried the suggestion below and now the database object I am using for the calculation is returning NULL. I changed the method from 'Sub' to 'Function'
Public Function CalculateTotal() As Integer
Dim objDatabase As BTTQAResults
Try
objDatabase = CType(Cache("QAResults"), BTTQAResults)
Dim calcTotal As Integer
For index As Integer = 0 To objDatabase.Count - 1
calcTotal = calcTotal + objDatabase(index).Mark
Next
calcTotal = CInt((calcTotal / objDatabase.Count) * 100)
Part1QAResult.DataSource = objDatabase
Part1QAResult.DataBind()
Catch ex As Exception
ExceptionManager.Publish(ex)
Response.Redirect("~/Error.aspx", False)
End Try
End Function
objDatabase = CType(Cache("QAResults"), BTTQAResults) is NULL
Upvotes: 0
Views: 274
Reputation: 12748
You might need to separate your logic into small method to make the code reusable. Or put it inside classes.
Public Function CalculateTotal() As Integer
' ...
End Function
Public Function IsTotalPassScore() As Boolean
Return CalculateTotal() >= 70
End Function
Public Sub BindGrid()
If IsTotalPassScore() Then
btnNext.Visible = True
Else
btnNext.Visible = False
End If
End Sub
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
If IsTotalPassScore() Then
Response.Redirect(mstrPassSlide, False)
End If
Response.Redirect(mstrFailSlide, False)
End Sub
If the calculation takes some time, it could be done in the OnInit and the result stored somewhere.
Upvotes: 0
Reputation: 37050
When you find yourself wanting to use a portion of logic from one method in another method, this usually indicates that the logic should be refactored into it's own method so it can be called from multiple places.
In this case we may be able to use two read-only properties, for example:
Public ReadOnly Property CalcTotal() As Integer
Get
'Do some calculation here
Return result
End Get
End Property
Public ReadOnly Property PassScore() As Boolean
Get
Return CalcTotal >= 70
End Get
End Property
Now either of these properties can be called from any method in the class.
Upvotes: 2
Reputation: 45
You should try to Call the BindGrid() method at your click event.
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
BindGrid()
If passScore = True Then
Response.Redirect(mstrPassSlide, False)
End If
'Response.Redirect(mstrFailSlide, False)
Catch ex As Exception
Throw ex
Finally
End Sub
Upvotes: 0