Solved

change boolean value

Posted on 2004-08-03
14
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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