Solved

change boolean value

Posted on 2004-08-03
14
377 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
Comment Utility
oops - disregard the commented blnStatus; it should not be commented out
0
 

Author Comment

by:matthersjr
Comment Utility
when I run it, the blnStatus remains false
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
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
Comment Utility
I ran your code and it works fine. The value was changed to True
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility

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
Comment Utility
baan,
it pretty much does the same


greg,
not sure i follow

0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
can you tell me, what exactly you are trying to do ?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Accepted Solution

by:
natloz earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
well the answer is exactly what I was saying, glad you got it.
0
 

Author Comment

by:matthersjr
Comment Utility
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
Comment Utility
lstData.Items.Clear()
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility

    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
Comment Utility
greg,

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

18 Experts available now in Live!

Get 1:1 Help Now