【问题标题】:For..Next, Exit For Not Working as Expected?For..Next,Exit for not working as expected?
【发布时间】:2011-03-21 16:42:40
【问题描述】:

当学生登录时,我会为他们加载一个仪表板,让他们了解他们在住宿预订过程中所处的位置。不管我做什么,尽管它似乎总是触发 Else 条件,即使我在 For..Next 循环中有一个 Exit For 。这是有问题的代码:

Public Sub Initialize_Dashboard()
    Dim term As String = CStr(Session("term"))
    Dim year As String = CStr(Session("year"))
    Dim people_code_id As String = CStr(Session("people_code_id"))
    Dim class_level As String = CStr(Session("class"))
    ' %%%%%% Show the resident's name and whether they have a room. %%%%%%
    Dim dbroom As New pbu_housingEntities
    Dim queryStudent = From p In dbroom.Residents _
                       Where p.people_code_id = people_code_id _
                       Join b In dbroom.Buildings On p.building Equals b.id _
                       Join r In dbroom.Rooms On p.room Equals r.id
                       Select p, b, r

    lblName.Text = CStr(Session("name"))
    If queryStudent.Count.Equals(0) Then
        lblRegistered.Text = "We do not have you currently registered for campus housing."
    Else
        lblRegistered.Text = "You are currently registered for " & queryStudent.First.b.building_name & " " & queryStudent.First.r.room1 & "."
    End If

    ' Initiate variables to check for class settings.
    Dim dbConfig As New pbu_housingEntities
    Dim whatdatest = From p In dbConfig.Configs
    Dim whatdatee = From p In dbConfig.Configs

    ' Add in a check for contract signatures.
    Dim dbContracts As New pbu_housingEntities
    Dim clcexists = From p In dbContracts.Signatures _
                    Where p.StudentID = people_code_id _
                    And p.ContractType = "clc" _
                    Order By p.ID Descending _
                    Select p
    Dim rhcexists = From p In dbContracts.Signatures _
                    Where p.StudentID = people_code_id _
                    And p.ContractType = "rhc" _
                    Order By p.ID Descending _
                    Select p
    ' Shows or hides the buttons for contracts based on whether signatures exist.
    Dim clcfirst = clcexists.FirstOrDefault()
    Dim rhcfirst = rhcexists.FirstOrDefault()
    If clcfirst Is Nothing Then
        pnlSignCLC.Visible = True
    Else
        pnlSignCLC.Visible = False
    End If
    If rhcfirst Is Nothing Then
        pnlSignRHC.Visible = True
    Else
        pnlSignRHC.Visible = False
    End If

    ' Determine if the student is eligible to register for class.
    Dim classes() As String = {"FR%", "SO", "JR", "SR", "SR5"}
    For Each value As String In classes
        ' Pull in the dates the student should be able to register for this class, compare them to the current date.
        Dim current_value = value
        If class_level = current_value Then
            Response.Write("hello")
            whatdatest = From p In dbConfig.Configs _
                         Where p.Description = current_value + "OD" _
                         Select p

            whatdatee = From p In dbConfig.Configs _
                        Where p.Description = current_value + "CD" _
                        Select p

            ' If the current date is within their registration period...
            If Date.Now >= whatdatest.First.dateValue And Date.Now <= whatdatee.First.dateValue Then
                Dim person_name As String = CStr(Session("person_name")) ' Must stay here or will conflict.
                Dim hasroom = From p In dbConfig.Residents _
                              Where p.person_name = person_name _
                              Where p.semester = term _
                              Where p.year = year _
                              Select p
                ' If they have signed their contracts, lets let them register for a room.
                If hasroom.Count.Equals(0) AndAlso clcfirst IsNot Nothing AndAlso rhcfirst IsNot Nothing Then
                    pnlBegin.Visible = True
                    btnBegin.Enabled = True

                    ' If they are already registered for a room, let them delete their reservation.
                ElseIf clcfirst IsNot Nothing AndAlso rhcfirst IsNot Nothing Then
                    pnlDelete.Visible = True
                    btnDelete.Enabled = True
                Else

                    ' In any other situation, we don't need to do anything.
                    ' There really shouldn't be any other situation.

                End If
                ' What to do if the current date is not within the room reservation window.
                Exit For
            Else
                Dim class_name As String
                Select Case current_value
                    Case "FR%"
                        class_name = "Freshman"
                    Case "SO"
                        class_name = "Sophmore"
                    Case "JR"
                        class_name = "Junior"
                    Case "SR"
                        class_name = "Senior"
                    Case "SR5"
                        class_name = "Fifth year Senior"
                End Select
                lblError.Text = "You are currently a " & current_value & ". You will be eligible to reserve a room between" _
                    & whatdatest.First.dateValue & " and " & whatdatee.First.dateValue & ". Please come back during those dates. Thanks!"
            End If
            Exit For
        Else
            ' What to do if the user isn't assigned a class level at all!
            lblError.Text = "Oops...Something might be wrong with our records. We don't have you registered for classes. Please contact Dave Mackey, x4543."
        End If
    Next

这是我将最后一个 else 子句移到单独的 If..Then 后的代码:

 Public Sub Initialize_Dashboard()
    Dim term As String = CStr(Session("term"))
    Dim year As String = CStr(Session("year"))
    Dim people_code_id As String = CStr(Session("people_code_id"))
    Dim class_level As String = CStr(Session("class"))
    ' %%%%%% Show the resident's name and whether they have a room. %%%%%%
    Dim dbroom As New pbu_housingEntities
    Dim queryStudent = From p In dbroom.Residents _
                       Where p.people_code_id = people_code_id _
                       Join b In dbroom.Buildings On p.building Equals b.id _
                       Join r In dbroom.Rooms On p.room Equals r.id
                       Select p, b, r

    lblName.Text = CStr(Session("name"))
    If queryStudent.Count.Equals(0) Then
        lblRegistered.Text = "We do not have you currently registered for campus housing."
    Else
        lblRegistered.Text = "You are currently registered for " & queryStudent.First.b.building_name & " " & queryStudent.First.r.room1 & "."
    End If

    ' Initiate variables to check for class settings.
    Dim dbConfig As New pbu_housingEntities
    Dim whatdatest = From p In dbConfig.Configs
    Dim whatdatee = From p In dbConfig.Configs

    ' Add in a check for contract signatures.
    Dim dbContracts As New pbu_housingEntities
    Dim clcexists = From p In dbContracts.Signatures _
                    Where p.StudentID = people_code_id _
                    And p.ContractType = "clc" _
                    Order By p.ID Descending _
                    Select p
    Dim rhcexists = From p In dbContracts.Signatures _
                    Where p.StudentID = people_code_id _
                    And p.ContractType = "rhc" _
                    Order By p.ID Descending _
                    Select p
    ' Shows or hides the buttons for contracts based on whether signatures exist.
    Dim clcfirst = clcexists.FirstOrDefault()
    Dim rhcfirst = rhcexists.FirstOrDefault()
    If clcfirst Is Nothing Then
        pnlSignCLC.Visible = True
    Else
        pnlSignCLC.Visible = False
    End If
    If rhcfirst Is Nothing Then
        pnlSignRHC.Visible = True
    Else
        pnlSignRHC.Visible = False
    End If

    ' Determine if the student is eligible to register for class.
    Dim classes() As String = {"FR%", "SO", "JR", "SR", "SR5"}
    Dim flag As String = "N"
    For Each value As String In classes
        ' Pull in the dates the student should be able to register for this class, compare them to the current date.
        Dim current_value = value
        If class_level = current_value Then
            flag = "Y"
            Response.Write("hello")
            whatdatest = From p In dbConfig.Configs _
                         Where p.Description = current_value + "OD" _
                         Select p

            whatdatee = From p In dbConfig.Configs _
                        Where p.Description = current_value + "CD" _
                        Select p

            ' If the current date is within their registration period...
            If Date.Now >= whatdatest.First.dateValue And Date.Now <= whatdatee.First.dateValue Then
                Dim person_name As String = CStr(Session("person_name")) ' Must stay here or will conflict.
                Dim hasroom = From p In dbConfig.Residents _
                              Where p.person_name = person_name _
                              Where p.semester = term _
                              Where p.year = year _
                              Select p
                ' If they have signed their contracts, lets let them register for a room.
                If hasroom.Count.Equals(0) AndAlso clcfirst IsNot Nothing AndAlso rhcfirst IsNot Nothing Then
                    pnlBegin.Visible = True
                    btnBegin.Enabled = True

                    ' If they are already registered for a room, let them delete their reservation.
                ElseIf clcfirst IsNot Nothing AndAlso rhcfirst IsNot Nothing Then
                    pnlDelete.Visible = True
                    btnDelete.Enabled = True
                Else

                    ' In any other situation, we don't need to do anything.
                    ' There really shouldn't be any other situation.

                End If
                ' What to do if the current date is not within the room reservation window.
                Exit For
            Else
                Dim class_name As String
                Select Case current_value
                    Case "FR%"
                        class_name = "Freshman"
                    Case "SO"
                        class_name = "Sophmore"
                    Case "JR"
                        class_name = "Junior"
                    Case "SR"
                        class_name = "Senior"
                    Case "SR5"
                        class_name = "Fifth year Senior"
                End Select
                lblError.Text = "You are currently a " & current_value & ". You will be eligible to reserve a room between" _
                    & whatdatest.First.dateValue & " and " & whatdatee.First.dateValue & ". Please come back during those dates. Thanks!"
            End If
            Exit For
        Else

        End If
    Next
    If flag = "N" Then
        ' What to do if the user isn't assigned a class level at all!
        lblError.Text = "Oops...Something might be wrong with our records. We don't have you registered for classes. Please contact Dave Mackey, x4543."
    End If

【问题讨论】:

  • 您是否调试并单步执行了代码以查看它在做什么?
  • 你有很多其他的。你能告诉我们是哪一个问题吗?
  • 由于 Exit For 位于 Date 内,因此请检查日期条件不得为真...
  • 它曾经 Response.Write("hello") 吗?如果不是,那么 class_level = current_value 在任何时候都不相同。
  • 会不会出现错误。是否在 Debug\Exceptions 中检查了 Common Language Runtime Exceptions Throw... 如果没有,请检查并再次运行以查看是否确实存在异常。

标签: asp.net vb.net loops


【解决方案1】:

我认为您需要删除最后一个 Else 语句

Else
            ' What to do if the user isn't assigned a class level at all!
            lblError.Text = "Oops...Something might be wrong with our records. We don't have you registered for classes. Please contact Dave Mackey, x4543."

否则你将永远无法通过所有选项。

你需要在循环之前设置一个“found”标志为false,如果有匹配则在循环内设置found为true,然后在循环之后设置error if found仍然为false。

【讨论】:

  • 我删除了 else 子句并添加了一个标志。但是现在,它只是将 lblError.Text 留空!我现在将更新后的代码附加到我的问题中......
  • @davemackey 我现在阅读的方式是,如果 class_level 匹配,那么如果日期匹配,则仅显示/隐藏面板,如果日期不匹配,请稍后再回来。如果在 class_level 上根本没有匹配,则说没有注册。
  • 是的,没错。唯一的问题是,如果日期不匹配,并不是说稍后再回来,它只是将 lblError.Text 留空。 lblError.Text 用于判断他们是否在课堂上且日期不匹配,以及如果他们根本不是学生。
  • 没关系!它现在正在工作(有点困惑,但我会继续使用它......)
【解决方案2】:

我可能会误解,但最可能的解释不是class_level &lt;&gt; current_value,这意味着执行直接转到Else 块? Exit For 语句被绕过。

编辑关注您的评论。您正在循环遍历数组 classes() 中的所有可能值,以检查匹配项。如果current_value 匹配first 值“FR%”,那么您肯定会到达Exit For。否则,您将转到您的Else 行“如果用户根本没有被分配班级级别该怎么办”

我想你可能没有考虑过你的逻辑?

【讨论】:

  • 是的,这是可能的——但是在调试器中观察它们是一样的,我知道它们都是字符串。
  • 查看我的编辑。您能否发布class_levelcurrent_value 的值
  • 你确定它们是一样的吗?包括任何空格等?你能用十六进制显示每个字符串并逐字节比较它们吗?
猜你喜欢
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2017-01-29
  • 2019-11-30
  • 1970-01-01
相关资源
最近更新 更多