-1

The code below shows the event when a button click is fire

Protected Sub btnFinish_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFinish.Click
         For i As Integer = 0 To Gridview1.Rows.Count - 1 Step i + 1
                Dim TextBox1 As TextBox = DirectCast(Me.Gridview1.Rows(i).FindControl("txtAnswer"), TextBox)
                If TextBox1.Text = String.Empty Then
                    'do something
                ElseIf TexBox1 <> String.Empty Then
                    'do something else
                End If
         Next
End Sub

The problem here is that the only condition being executed is in the If-statement even if it should execute the ElseIf-statement. Can someone explain why and how can I solve this problem? [EDITED]

eirishainjel
  • 570
  • 7
  • 25
  • Are you sure the condition for `else` is matched? – King King Dec 09 '13 at 02:51
  • Change `ElseIf an.Text = String.Empty Then` to just `Else`. Because the `If` checks that `an.Text` is not empty, by reaching the `Else` it must be empty. – musical_coder Dec 09 '13 at 02:54
  • Sorry I'm not as familiar with c# as other languages, but isn't the '=' an assignment operator, and you want to use '==' – Jags Dec 09 '13 at 02:55
  • @MattQuinlan in `VB.NET` the `=` can be used as both assignment and equal comparison operator. – King King Dec 09 '13 at 02:57
  • @musical_coder I've already done it, it only reads the Else condition even if it should be in If condition. – eirishainjel Dec 09 '13 at 02:59
  • @eirishainjel when you did like as musical_coder suggested, there won't any different result **but** that's how you should write the `IF-ELSE`, the `ELSE` condition you provided is **redundant**. – King King Dec 09 '13 at 03:00
  • @eirishainjel do you intend to execute the for loop until you find some `TextBox` with `Text` not being empty? – King King Dec 09 '13 at 03:08
  • @KingKing The condition is when there is an empty textbox is gridview, it will get the index of the gridview row with an empty textbox. When there's no textbox that is empty, it will call a javascript function. – eirishainjel Dec 09 '13 at 03:11

1 Answers1

2

a couple of things to note when comparing text/string:

  1. String could be NULL, instead of Empty
  2. use String.IsNULLOrEmpty to check NULL/Empty
  3. String could be WhiteSpace too, use String.IsWhiteSpace to check it
  4. User could enter a few spaces in some cases, if you want to make sure it's correct, use String.Trim to eliminate any unwanted spaces.

normally what I do is: (NOT String.IsNULLOrEmpty(givenText)) AndAlso givenText.Trim.Length <> 0

Rex
  • 2,130
  • 11
  • 12
  • @eirishainjel that can't be your problem, you should not make thing confusing that way. `String.Empty` is equal to `""`. – King King Dec 09 '13 at 03:23
  • Really? But why when I changed it the `String.Empty` into `""`, the if-else condition works. I was also confused because I always use `String.Empty` in other codes and it works well. Just now that when I sue it in the if-else, it doesn't work. – eirishainjel Dec 09 '13 at 03:29
  • 1
    look at the comparison of the two: http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and-empty-string – Rex Dec 09 '13 at 03:33
  • 1
    @eirishainjel I'm sure that you will encounter some problem with your code in future. It's so messy, and the `Step` of the for loop is strange, If you want to loop through all the rows, the `Step` should be `1` not `a+1`, and in that case we can omit the optional `Step`. From the link given by `Rex`, it's clear that they are the same, the only difference is `String.Empty` does not create a new object but comparison between the two should be the same. I hope you will review your code, try it again and comment back, even delete your comment to avoid confusing others. – King King Dec 09 '13 at 04:38
  • On top of King King's comment, i would want to emphasize the performance (though not many people really care about it nowadays...) - do think a bit more about the performance: comparing with String.Empty or "" could be slower. Use standard method would not only make the code more readable, but also improve the performance – Rex Dec 09 '13 at 04:42
  • Okay, I deleted that comment Sir. I also edit the question to make it clear. – eirishainjel Dec 09 '13 at 04:52