Solved

change boolean value

Posted on 2004-08-03
14
381 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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
 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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