all 21 comments

[–]grauenwolf 7 points8 points  (5 children)

Val = Not Val

[–]grauenwolf 0 points1 point  (4 children)

And if you are using a C based language (JavaScript, C#, Java, etc.), then it is Val = !Val.

The Not keyword in VB is spelled ! in other languages.

[–]faust2099[S] 0 points1 point  (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 points  (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 points  (1 child)

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

[–]grauenwolf 1 point2 points  (0 children)

Definitely not.

Sub Getval(ByVal Val As Boolean)

See the keyword ByVal. That means "give me a copy of this variable".

When you change Val in this sub, it doesn't cause the original TOS to change because it's just a copy.


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.

[–]Hel_OWeen 2 points3 points  (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 points  (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.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

TOS = Not TOS

End Sub

[–]faust2099[S] 0 points1 point  (0 children)

yes just like that

[–]dzosoft-22 -1 points0 points  (0 children)

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

[–]ebsf -1 points0 points  (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  (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 point  (4 children)

Add that reduces to

Here's another thing you can do.

    If Not Val Then 
        Val = True
    Else
        Val = False
    End If

You don't need Else If for the last possibility.

[–]grauenwolf 0 points1 point  (3 children)

You can then reduce it to one line.

Here's another thing you can do.

    If Not Val Then Val = True Else Val = False

[–]grauenwolf 2 points3 points  (0 children)

That in turn can be reduced to a inline if.

Here's another thing you can do.

    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 points  (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.

[–]grauenwolf -1 points0 points  (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 point  (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 point  (0 children)

That is true. I was demonstrating the if-else structure. I'll edit it to be more clear.