locked
Boolean Variable Strange RRS feed

  • Question

  • 1. in statement 1 i set a bool variable value to true

    2. in statement 2: if variable is true then stop

    The code does not stop in run mode. (I set a stop right after line 2 in order to check the variables state)

    The variables state is false in spite of being set to true at line 1

    i stop the code before line 1 and single step through it. The variable state is now true and execution executes the stop.

    I normally try to fix it myself but i don't have a answer to this.

    Stop: Sorry for the Post. I had a msgbox prior to the code and clicking on the msgbox was firing a form Activated event that was keeping my variable from being set to true. How it got to be true during single stepping is still a mystery to me but that's another story since it's no longer a issue. Thanks anyway.


    • Edited by LGB54 Tuesday, October 27, 2020 5:10 PM
    Tuesday, October 27, 2020 4:31 PM

All replies

  • Hi

    Show the code - all 3 lines.


    Regards Les, Livingston, Scotland

    Tuesday, October 27, 2020 5:02 PM
  • Stop: Sorry for the Post. I had a msgbox prior to the code and clicking on the msgbox was firing a form Activated event that was keeping my variable from being set to true. How it got to be true during single stepping is still a mystery to me but that's another story since it's no longer a issue. Thanks anyway.
    Tuesday, October 27, 2020 5:12 PM
  • As noted, might want to share at least a few lines of the code you have.

    It is possible that the code to set/update/change the Boolean value does not get a chance to run since the loop or code is BUSY.

    Take a quick read of this recent post - it will give you the keys and concepts you need to make this work:

    loop interruption (microsoft.com)

    Regards,

    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    Tuesday, October 27, 2020 6:59 PM
  • Thanks for replying.

    If i set the bool value to true before the msgbox statement it returns true

    If i try to set the bool value to true after the msgbox statement it never executes because

    unknown to me at the time clicking yes or no in the message box triggers a form_Activate

    event and setting the bool value to true line never executes. The form Activate Event was causing unexpected results. Removing the form activate event is not an option so i'm working on a work around.

    Thanks again.

    Tuesday, October 27, 2020 11:47 PM
  • Hi LGB54,

    Thanks for your feedback.

    >>at the time clicking yes or no in the message box triggers a form_Activate event and setting the bool value to true line never executes.

    We need some related code to analyze your problem and make a test.

    You can insert the code by using 'Insert Code Block'.

    Besides, check if following code works for you.

        Private bol As Boolean = False
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Task.Run(Sub()
                         Dim dialogResult = MsgBox("Message", vbOKCancel, "Menu")
                         If dialogResult = MsgBoxResult.Ok Then
                             'form_Activate
                         Else
                             '...
                         End If
                     End Sub)
            bol = True
            '...
        End Sub

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 28, 2020 2:19 AM
  •  Sub UpdateBeatfile()
            Static A1, A2
            Static LastItemValue As Single
    
            UpdatingString = "Updating"
            'UpdatingTable = True
            Ans3 = MsgBox("Line 933 _ Save Changes to " & AutoFetch, vbYesNo)
            'If i Click Yes or No on above Line (941) it executes a form activate event.
            'when it returns, it returns to the "Ans3 = MsgBox" Line creating a endless loop.
            'my thinking was that the code should resume after the msgbox line, not at the msgbox line.
            'I had about 20 msgboxes saying the same thing.
    
            'Because of that loop the next line "If Ans3 = vbno then" never executes
            If Ans3 = vbNo Then
                ListBox1.Items.Clear() : ListBox1.Visible = False
                ListBox2.Items.Clear() : ListBox2.Visible = False
    
                Ans3 = 0 : UpdatingString = " "
                CancelToolStripMenuItem_Click(A1, A2)
                'Exit Sub
            End If
    
            'Note: I was also erroneously trying to read a string from listbox1 into a Variable
            'diminsioned as single. But instead of getting an error it triggered another form Activate event.
            'when i corrected this problem i no longer triggered the form activate event.
            'That code was: "LastItemValue = Listbox1.SelectedItem" this would trigger a form activate event.
    

    Wednesday, October 28, 2020 1:17 PM
  • Hi

    Here is some code performing what you show and no issues with it.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UpdateBeatfile()
      End Sub
      Sub UpdateBeatfile()
        Dim Ans3 As DialogResult = MessageBox.Show("Line 8 _ Save Changes to " & "AutoFetch", "CONFIRM", MessageBoxButtons.YesNo)
        If Ans3 = vbNo Then
          ' it does stop here if user clicks NO
          Stop
        Else
          ' it does stop here if user clicks YES
          Stop
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Proposed as answer by dbasnett Wednesday, October 28, 2020 4:29 PM
    Wednesday, October 28, 2020 1:55 PM
  • The code for all appearances seems to be working as it should so i'm leaving it alone, but i still don't

    really understand. I did try your working code and attached the result.

    Sub InputTest1()
            Dim Ans4 As DialogResult = MessageBox.Show("Line 8 _ Save Changes to " & "AutoFetch", "CONFIRM", MessageBoxButtons.YesNo)
            'The Messagebox displays for about 1 second then with no User input another sub, not the "Form Activated" sub
            'Starts to execute
            Stop 'This line never executes, execution has been tranfered to another sub
    
            If Ans4 = vbNo Then
                ' it does stop here if user clicks NO
                'Stop
            Else
                ' it does stop here if user clicks YES
                ' Stop
            End If
        End Sub

    Wednesday, October 28, 2020 11:15 PM
  • The code for all appearances seems to be working as it should so i'm leaving it alone, but i still don't

    really understand. I did try your working code and attached the result.

    Sub InputTest1()
            Dim Ans4 As DialogResult = MessageBox.Show("Line 8 _ Save Changes to " & "AutoFetch", "CONFIRM", MessageBoxButtons.YesNo)
            'The Messagebox displays for about 1 second then with no User input another sub, not the "Form Activated" sub
            'Starts to execute
            Stop 'This line never executes, execution has been tranfered to another sub
    
            If Ans4 = vbNo Then
                ' it does stop here if user clicks NO
                'Stop
            Else
                ' it does stop here if user clicks YES
                ' Stop
            End If
        End Sub

    Hi

    Your issue is somewhere else.  You say another sub is executing 'on it's own' - not so! Something else is causing it to run - like a Timer somewhere, or another thread running in the background - something else in your code is happening and you are seeing the result in this non related code.  Unless you post all of your code, then no-one would likely be in a position to help any further.

    BTW: the code you posted DOES work as expected here.


    Regards Les, Livingston, Scotland



    • Edited by leshay Wednesday, October 28, 2020 11:44 PM
    Wednesday, October 28, 2020 11:43 PM
  • Thought about the Timer thing so i disabled all timers and ran this sub as the very 1st line of code. Same result. But thanks for the help, it gave me a different way of looking at the code and it all seems to work right now. So i'm leaving it at that. Thanks again.
    Thursday, October 29, 2020 12:06 AM
  • Hi LGB54,

    >>it all seems to work right now

    If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 29, 2020 7:24 AM