Solved

change boolean value

Posted on 2004-08-03
14
378 Views
Last Modified: 2010-04-23
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
---
0
Comment
Question by:matthersjr
  • 5
  • 4
  • 2
  • +3
14 Comments
 

Author Comment

by:matthersjr
ID: 11705955
oops - disregard the commented blnStatus; it should not be commented out
0
 

Author Comment

by:matthersjr
ID: 11705965
when I run it, the blnStatus remains false
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11706416
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 11706493
I ran your code and it works fine. The value was changed to True
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11706515

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
 

Author Comment

by:matthersjr
ID: 11706629
baan,
it pretty much does the same


greg,
not sure i follow

0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11706700
can you tell me, what exactly you are trying to do ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Accepted Solution

by:
natloz earned 125 total points
ID: 11706718
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11706998
I agree with the module-level approach.  Another way would be to use a Static variable:

Static blnStatus As Boolean

Bob
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11707745
well the answer is exactly what I was saying, glad you got it.
0
 

Author Comment

by:matthersjr
ID: 11707848
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11708025
lstData.Items.Clear()
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11708045

    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
 

Author Comment

by:matthersjr
ID: 11708068
greg,

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Limiting string to two deciamls 18 49
VB.Net - CSV to Oracle table 4 51
VB.Net - For Loop Error 5 31
vb.net 2 37
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now