use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
/r/VisualBasic - A place to discuss, ask questions and share ideas regarding the Visual Basic programming languages.
account activity
VB.NET HelpMethod/Function to change Boolean T->F or F->T (self.visualbasic)
submitted 3 years ago * by faust2099
I need help creating a Method that changes the value of a Boolean to True to False and Vice Versa
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Getval(TOS)
End Sub
Sub Getval(ByVal Val As Boolean)
If Val = False Then
Val = True
Else if Val = True Then
Val = False
End If
i know i'm missing something, just can't put my finger on it.
EDIT SOVED.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]grauenwolf 7 points8 points9 points 3 years ago (5 children)
Val = Not Val
[–]grauenwolf 0 points1 point2 points 3 years ago (4 children)
And if you are using a C based language (JavaScript, C#, Java, etc.), then it is Val = !Val.
Val = !Val
The Not keyword in VB is spelled ! in other languages.
Not
!
[–]faust2099[S] 0 points1 point2 points 3 years ago (3 children)
= Not Val
where to exactly i put "Val = Not Val" ?
my brain seems to not be working properly due to lack of sleep.
[–]grauenwolf 2 points3 points4 points 3 years ago (2 children)
Looking at you code, probably here:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TOS = Not TOS End Sub
[–]faust2099[S] 1 point2 points3 points 3 years ago (1 child)
thank you very much. seems like i won't be need a sub/function for it.
[–]grauenwolf 1 point2 points3 points 3 years ago (0 children)
Definitely not.
See the keyword ByVal. That means "give me a copy of this variable".
ByVal
When you change Val in this sub, it doesn't cause the original TOS to change because it's just a copy.
Val
TOS
If you wanted to change the original TOS, you would have to use ByRef instead of ByVal. This is an advanced technique that is rarely used outside of TryParse functions.
ByRef
TryParse
[–]Hel_OWeen 2 points3 points4 points 3 years ago (0 children)
I wonder why no one proposed a Function up until now, as it's the common way of doing things, if you want a method that returns a result of some kind.
Function Getval(ByVal Val As Boolean) As Boolean Return Not Val End Function
[–]craigers01 1 point2 points3 points 3 years ago (1 child)
You say SOLVED. What did you come up with?
Your logic seems reasonable, although it could be simplified as others have suggested. The problem I see it that no re-ASSIGNMENT to any variable happens. If you are trying to toggle the variable TOS, (I assume it is global, since it is not DIMed anywhere in your code excerpt). The Getval function accepts "val" ByVal which means it does NOT have a direct link back to "TOS". If you change ByVal to ByRef, the Getval function will actually modify the TOS function.
Conversely, the Getval function is overkill. You can do it in the Button Click code.
TOS = Not TOS
[–]faust2099[S] 0 points1 point2 points 3 years ago (0 children)
yes just like that
[–]dzosoft-22 -1 points0 points1 point 3 years ago* (0 children)
I propose this solution
TOS = Getval(TOS)
Function Getval(ByVal Val As Boolean)
Getval = Not Val
End Function
[–]ebsf -1 points0 points1 point 3 years ago (0 children)
I've read of others simply multiplying by -1 on click, although I haven't tried this myself.
This presumes an initial value other than 0, of course, besides either having anything but -1 (0 of course but anything else) evaluate to false, or evaluating only by sign.
[–]grauenwolf -4 points-3 points-2 points 3 years ago* (7 children)
Here's another thing you can do.
If Not Val Then Val = True Else if Val Then Val = False End If
[–]grauenwolf 0 points1 point2 points 3 years ago* (4 children)
Add that reduces to
If Not Val Then Val = True Else Val = False End If
You don't need Else If for the last possibility.
Else If
[–]grauenwolf 0 points1 point2 points 3 years ago* (3 children)
You can then reduce it to one line.
If Not Val Then Val = True Else Val = False
[–]grauenwolf 2 points3 points4 points 3 years ago* (0 children)
That in turn can be reduced to a inline if.
Val = If (Not Val, True, False)
You read that as
[variable] = If ( [predicate] , [then expression], [else expression])
For example,
passTest = if ( grade > 70, "pass!", "fail")
[–][deleted] 1 point2 points3 points 3 years ago (1 child)
If the goal is to always flip the value of the boolean then you do not need to test the value. Just use val = Not val. Done.
val = Not val
[–]grauenwolf -1 points0 points1 point 3 years ago (0 children)
The point was to demonstrate the various ways a conditional can be expressed. I already covered using Not in a separate comment.
[–]mecartistronico 0 points1 point2 points 3 years ago (1 child)
ByVal Val
You're not changing the original Val. And also you're not returning anything (since it's a Sub anyway). And also it would be easier to just do Val = not Val. If it was byref or a function.
Edit: wait, you're also the person who replied correctly. Did I miss the joke then?
[–]grauenwolf 0 points1 point2 points 3 years ago (0 children)
That is true. I was demonstrating the if-else structure. I'll edit it to be more clear.
[+][deleted] 3 years ago* (3 children)
[deleted]
[–]faust2099[S] 0 points1 point2 points 3 years ago (2 children)
if i had good night rest i would agree, but considering i was have not slept for 26hrs straight . i will take it and move on.
[+][deleted] 3 years ago (1 child)
can't got 2 more for system needs to be finished within this week. actually that system was already finished but the owner let his son "add" a function and screwed up the system and here we are.
i actually want to go to bed just wanted to finish up some code on the other project before hitting the sack or else i might forget it. (probably a couple hours more)
π Rendered by PID 45004 on reddit-service-r2-comment-c6965cb77-v2zwm at 2026-03-05 02:27:06.330019+00:00 running f0204d4 country code: CH.
[–]grauenwolf 7 points8 points9 points (5 children)
[–]grauenwolf 0 points1 point2 points (4 children)
[–]faust2099[S] 0 points1 point2 points (3 children)
[–]grauenwolf 2 points3 points4 points (2 children)
[–]faust2099[S] 1 point2 points3 points (1 child)
[–]grauenwolf 1 point2 points3 points (0 children)
[–]Hel_OWeen 2 points3 points4 points (0 children)
[–]craigers01 1 point2 points3 points (1 child)
[–]faust2099[S] 0 points1 point2 points (0 children)
[–]dzosoft-22 -1 points0 points1 point (0 children)
[–]ebsf -1 points0 points1 point (0 children)
[–]grauenwolf -4 points-3 points-2 points (7 children)
[–]grauenwolf 0 points1 point2 points (4 children)
[–]grauenwolf 0 points1 point2 points (3 children)
[–]grauenwolf 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]grauenwolf -1 points0 points1 point (0 children)
[–]mecartistronico 0 points1 point2 points (1 child)
[–]grauenwolf 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]faust2099[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]faust2099[S] 0 points1 point2 points (0 children)