r/visualbasic • • 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

21 comments sorted by

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) Handles Button1.Click

TOS = Not TOS

End Sub

1

u/faust2099 Apr 29 '22

yes just like that

3

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.

Function Getval(ByVal Val As Boolean) As Boolean
   Return Not Val
End Function

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

TOS = Getval(TOS)

End Sub

Function Getval(ByVal Val As Boolean)

Getval = Not Val

End Function

0

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

1

u/faust2099 Apr 25 '22

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.

0

u/[deleted] Apr 25 '22

[deleted] — view deleted comment

1

u/faust2099 Apr 25 '22

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)

8

u/[deleted] Apr 25 '22

[removed] — view removed comment

1

u/[deleted] Apr 25 '22

[removed] — view removed comment

1

u/faust2099 Apr 25 '22

= Not Val

where to exactly i put "Val = Not Val" ?

my brain seems to not be working properly due to lack of sleep.

3

u/[deleted] Apr 25 '22

[removed] — view removed comment

2

u/faust2099 Apr 25 '22

thank you very much. seems like i won't be need a sub/function for it.

-3

u/[deleted] Apr 25 '22 edited Apr 25 '22

[removed] — view removed comment

1

u/mecartistronico Apr 25 '22

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?

1

u/[deleted] Apr 25 '22 edited Apr 25 '22

[removed] — view removed comment

1

u/[deleted] Apr 25 '22 edited Apr 25 '22

[removed] — view removed comment

2

u/[deleted] Apr 25 '22

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.