If else inside a loop in VB.Net

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]

Answers


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


Need Your Help

Quick-fixing 32-bit (2GB limited) fseek/ftell on freebsd 7

c freebsd large-files fseek

I have old 32-bit C/C++ program on FreeBSD, which is used remotely by hundreds of users, and author of which will not fix it. It was written in unsafe way, all file offset are stored internally as

Finding first and last index of some value in a list in Python

python list search

Is there any built-in methods that are part of lists that would give me the first and last index of some value, like:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.