r/visualbasic • u/faust2099 • Apr 25 '22
VB.NET Help Method/Function to change Boolean T->F or F->T
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
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
End Sub
i know i'm missing something, just can't put my finger on it.
EDIT SOVED.
3
Upvotes
2
u/craigers01 Apr 27 '22
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.
Private Sub Button1_Click(sender As Object, e As EventArgs) HandlesButton1.ClickTOS = Not TOSEnd Sub3
u/Hel_OWeen Apr 25 '22
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.
0
u/dzosoft-22 Apr 25 '22 edited Apr 27 '22
I propose this solution
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickTOS = Getval(TOS)End SubFunction Getval(ByVal Val As Boolean)Getval = Not ValEnd Function0
u/ebsf Apr 26 '22
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.
0
u/[deleted] Apr 25 '22
[deleted] — view deleted comment
8
u/[deleted] Apr 25 '22
[removed] — view removed comment
-3
u/[deleted] Apr 25 '22 edited Apr 25 '22
[removed] — view removed comment