Vb "If" statement characters in textbox

Douglas Gariepy 6 Reputation points
2022-12-05T17:42:41.243+00:00

So I'm just learning Visual Basic and I'm only getting my Else statement as a return here is my code

Private Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnClick.Click
Dim grTot As Integer
grTot = CDec(Grd1txt.Text) + CDec(Grd2txt.Text) + CDec(Grd3txt.Text)
Dim grAvg As Integer
grAvg = grTot / 3
If Mjrtxt.Text = "CI" And grAvg > 70 Then
Statxt.Text = "Passing"
If Mjrtxt.Text = "CI" And grAvg < 71 Then
Statxt.Text = "Failing"
Else
Statxt.Text = "Not a CI major"
End If
End If
End Sub

Developer technologies | VB
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Douglas Gariepy 6 Reputation points
    2022-12-05T20:00:13.557+00:00

    @LesHay

    I partially understood your answer, I'm still very new. I had debug showing but in the code I posted it showed zero problems though I will admit when it does show problems I only partially understand what it's telling me. It looks like my problem wasn't even what I thought it was, I thought it was not recognizing the characters "CI" in the Mjrtxt.Text textbox but it was, it was the way I had my If's and Else's laid out that it was skipping a chunk of my code all together. This is how I changed it to work exactly as I was trying.

        Private Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnClick.Click  
            Dim grTot As Integer  
            grTot = CDec(Grd1txt.Text) + CDec(Grd2txt.Text) + CDec(Grd3txt.Text)  
            Dim grAvg As Integer  
            grAvg = grTot / 3  
            If Mjrtxt.Text = "CI" Then  
                If grAvg > 70 Then  
                    Statxt.Text = "Passing"  
                Else  
                    Statxt.Text = "Failing"  
                End If  
            Else  
                Statxt.Text = "Not a CI major"  
            End If  
        End Sub  
    
    1 person found this answer helpful.
    0 comments No comments

  2. LesHay 7,141 Reputation points
    2022-12-05T17:50:28.11+00:00

    Hi
    You have not asked a question.
    You say you are only getting the result from the 'ELSE' side of the loop and I would supose you are expecting a different result.

    I suggest you put a break point on the line in the 'ELSE' part [Statxt.Text = "Not a CI major"], run in Debug mode and when the break is reached hover your mouse pointer over the various variables to see what their values are - probably one or more will not be what you think they should be.

    EDIT: also, please use the code block from the toolbar to post code.

    0 comments No comments

  3. Dewayne Basnett 1,381 Reputation points
    2022-12-05T20:33:02.917+00:00

    Since you fixed the error take a look at this,

            Dim grTot As Integer '<<<<<  integer  
            Dim gr1 As Integer = 0  
            Dim gr2 As Integer = 0  
            Dim gr3 As Integer = 0  
            If Not (Integer.Parse(Grd1txt.Text, gr1) AndAlso  
                Integer.Parse(Grd2txt.Text, gr2) AndAlso  
                Integer.Parse(Grd3txt.Text, gr3)) Then  
                'input error  
            Else  
                grTot = gr1 + gr2 + gr3  
                Dim grAvg As Double  
                grAvg = grTot / 3 'produces double  
                If Mjrtxt.Text = "CI" Then  
                    If grAvg > 70.0R Then 'gretaer than or greater than or equal?????  
                        Statxt.Text = "Passing"  
                    Else  
                        Statxt.Text = "Failing"  
                    End If  
                Else  
                    Statxt.Text = "Not a CI major"  
                End If  
            End If  
      
    
    0 comments No comments

  4. LesHay 7,141 Reputation points
    2022-12-05T20:43:15.447+00:00

    Hi

    At the beginning, until you become aquaint with the system, it can be difficult to layout a set of interlocking If ..... Then blocks. Here is an alternative that you might like better. The big advantage with this is that it breaks things into manageable blocks, and, adding other checks in the Select...End Select is much more easily done.

    Did you manage to use the Break Point(s) I mentioned?

    You would really benifit putting these 2 Options at the very top of your code. This stops you from entering (some of) the more common error producing code lines and forces you to write more pleasing code.

    Option Strict On
    Option Explicit On

      Select Case Mjrtxt.Text  
          Case "CI"  
            If grAvg > 70 Then  
              Statxt.Text = "Passing"  
            Else  
              Statxt.Text = "Failing"  
            End If  
          Case Else  
            Statxt.Text = "Failing"  
        End Select  
      
    

    ' if those checks are all that is required then only 2 lines are needed:

    Statxt.Text = "Failing
    If grAvg > 70 Then Statxt.Text = "Passing"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.