简体   繁体   中英

Visual Basic 2010 For ..To Statment

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim password As String, ch As Char
    Dim i As Integer
    Dim valid As Boolean = False
    While valid = False
        password = InputBox("Please enter  your password")
        For i = 0 To password.Length - 1
            ch = password.Chars(i)
            If Not Char.IsLetterOrDigit(ch) Then
                valid = True
                Exit For
            End If
        Next
        If valid Then
            MsgBox("Your new password will be activated immediately")
        Else
            MsgBox("your password must contain at least one special symbol")
        End If
    End While
End Sub

Hello all, this program will check if there is a symbol within the password ,my question is in the statment ( For i = 0 To password.Length - 1 ) its about (-1) why did we write -1,i understood everything except this -1 , thanks

Right i got it , you are right . Its because vb starts counting from 0 , so if i write 1234 this mean for vb 12345 and then we should remove the last digit by -1 and then we beocome 1234 as result. Thank you Hans Passant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM