change boolean value

I've got the following and can not change the status of a boolean variable from false to true. Any ideas?
---
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        'declare variable for loop
        Dim n As Integer
        Dim blnStatus As Boolean

        blnStatus = False
        MessageBox.Show(blnStatus, "blnStatus Value")

        If blnStatus = False Then
            'do the loop when the button is clicked
            'run through the loop 5 times
            For n = 1 To 5
                'add the item to the list
                lstData.Items.Add("I'm item " & n & " in the list")
            Next
            'after running once, set the blnstatus to True
            'blnStatus = True
        Else
            'if blnstatus is set to True then display this message and stop running the action of the button
            lstData.Items.Add("Sorry, I can not run anymore")
        End If
       
    End Sub
---
matthersjrAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
natlozConnect With a Mentor Commented:
Basically if this is your form...you need to make this variable global to all of your form controls while the form is open...eg.
--------------------------------
Option Explicit On

'Import required Namespaces
Imports System.Data
Imports System.Data.SqlClient

Public Class frmMain
    Inherits System.Windows.Forms.Form

    'GLOBAL BOOLEAN VALUE
    Private blnStatus as boolean = false

Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        'declare variable for loop
        Dim n As Integer
        MessageBox.Show(blnStatus, "blnStatus Value")

        If blnStatus = False Then
            'do the loop when the button is clicked
            'run through the loop 5 times
            For n = 1 To 5
                'add the item to the list
                lstData.Items.Add("I'm item " & n & " in the list")
            Next
            'after running once, set the blnstatus to True
            blnStatus = True
        Else
            'if blnstatus is set to True then display this message and stop running the action of the button
            lstData.Items.Add("Sorry, I can not run anymore")
        End If
    End Sub
End Class
0
 
matthersjrAuthor Commented:
oops - disregard the commented blnStatus; it should not be commented out
0
 
matthersjrAuthor Commented:
when I run it, the blnStatus remains false
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
DotNetLover_BaanCommented:
hmm... If loop is not a good selection here... Go for while loop...

        Dim n As Integer
        Dim blnStatus As Boolean
        blnStatus = False
        MessageBox.Show(blnStatus, "blnStatus Value")

        While blnStatus = False
            'do the loop when the button is clicked
            'run through the loop 5 times
            For n = 1 To 5
                'add the item to the list
                lstData.Items.Add("I'm item " & n & " in the list")
            Next
            'after running once, set the blnstatus to True
            blnStatus = True
        End While
        'if blnstatus is set to True then display this message and stop running the action of the button
        lstData.Items.Add("Sorry, I can not run anymore")

-Baan
0
 
iboutchkineCommented:
I ran your code and it works fine. The value was changed to True
0
 
gregoryyoungCommented:

im assuming that you want it to run the first time you click the button and then not do it again in the future ?

if that is the case ...

get rid of
       Dim blnStatus As Boolean
        blnStatus = False
and in your class put ...

private blnStatus as boolean = false ...


then your first time through you will set the boolean to true and it will retain its value for the second time through the function.
0
 
matthersjrAuthor Commented:
baan,
it pretty much does the same


greg,
not sure i follow

0
 
DotNetLover_BaanCommented:
can you tell me, what exactly you are trying to do ?
0
 
Bob LearnedCommented:
I agree with the module-level approach.  Another way would be to use a Static variable:

Static blnStatus As Boolean

Bob
0
 
gregoryyoungCommented:
well the answer is exactly what I was saying, glad you got it.
0
 
matthersjrAuthor Commented:
greg,
I'm new at vb.net so I needed a little more help.
--------

What if I wanted to reset boolean and clear/erase the list box

    Private Sub btnRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestart.Click
        'reset boolean value to false
        blnStatus = False
        'clear the listbox lstData
        lstData.Focus()
        lstData.Select()
        lstData.ClearSelected()
    End Sub

I tried the above and it did not clear it. What would I use?

Thanks
0
 
gregoryyoungCommented:
lstData.Items.Clear()
0
 
gregoryyoungCommented:

    Private Sub btnRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestart.Click
        'reset boolean value to false
        blnStatus = False
        'clear the listbox lstData
        lstData.Items.Clear()
    End Sub
 
 
0
 
matthersjrAuthor Commented:
greg,

thanks; i thought i was on the right track just did not specify the items
0
All Courses

From novice to tech pro — start learning today.