1

I was trying to pass a boolean and assign a value (true/false) to it after passing it to another method

I want it to work with multiple booleans, For example:

'My.Settings.boolean1 = False
'My.Settings.boolean2 = True

Private Sub Button1_Click() Handles Button1.Click
    DoSomething(My.Settings.boolean1)
End Sub
Private Sub Button2_Click() Handles Button2.Click
    DoSomething(My.Settings.boolean2)
End Sub

Public Shared Sub DoSomething(aBoolean As Boolean) 'BooleanName As String
   if aBoolean then 
        aBoolean = False '(Sets locally not the main boolean)
        'etc...
   Else
        aBoolean = True
        'etc...
   End If
End Sub 

It assigns the value to aBoolean and does not actually touch the original boolean, which is the target

(clearly it doesn't work that way), so

I tried to use a string to pass the boolean name to DoSomething(aBoolean As Boolean, BooleanName As String) so I can use the boolean to know the condition, and the string to know the boolean name to assign using Convert.ToBoolean(BooleanName)

.. It did not work too

Is it even possible to do such a thing? I feel yes but I'm still trying to wrap my head around it

Real code example:

Private Sub PopupMessages_Button_Click(sender As Object, e As EventArgs) Handles PopupMessages_Button.Click
    Button_MouseClick(PopupMessages_Button, My.Settings.PopupMessages, ToolTip, PopupMessages_Button_Tooltip)
End Sub

Public Shared Sub Button_MouseClick(aButton As Button, aBoolean As Boolean, aToolTip As ToolTip, aToolTip_txt As String)
    If aBoolean Then
        aButton.BackColor = Color.WhiteSmoke
        aBoolean = False 
        aToolTip.SetToolTip(aButton, "(Disabled) " + aToolTip_txt)
    Else
        button.BackColor = Color.Green
        aBoolean = True 
        aToolTip.SetToolTip(aButton, "(Enabled) " + aToolTip_txt)
    End If
    My.Settings.Save()
End Sub
Galacticai
  • 87
  • 1
  • 13
  • 1
    You need to use [`ByRef`](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/byref). Read this for more info: [Passing Arguments by Value and by Reference](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/passing-arguments-by-value-and-by-reference). – 41686d6564 stands w. Palestine Oct 05 '18 at 14:31
  • Or simply invert the value of used my.settings – CruleD Oct 05 '18 at 14:33
  • @Ahmed Abdelhameed I'm speechless, I mean how did something so simple go over my head so easily... `ByRef` Worked perfectly. Thanks dude! – Galacticai Oct 05 '18 at 15:23

2 Answers2

1

It is possible. You can do so by making it a ByRef parameter. For instance

Private Sub Button1_Click() Handles Button1.Click
    DoSomething(My.Settings.boolean1)
End Sub

Public Shared Sub DoSomething(ByRef aBoolean As Boolean)
    aBoolean = Not aBoolean
End Sub 

It wasn't an entirely crazy idea to think that it would work to use a String, instead of a Boolean, since String is reference type. However, despite it being a reference type, strings are designed to be immutable and they are a bit of a special case. So, in effect, it kind of acts like a value type, even though it is technically a reference type. If you had used a mutable reference type, like StringBuilder, that would have worked (though I'm not suggesting it--ByRef is obviously preferable).

That being said, it's worth mentioning that, except in special cases, ByRef arguments are generally discouraged by most people, since it's not usually obvious. When you're reading the code, you can't easily tell that the method changes the value of the argument (it's more obvious in C# because you must specify out at each call-site). It's generally recommended that new values are returned by the method, instead:

Private Sub Button1_Click() Handles Button1.Click
    My.Settings.boolean1 = DoSomething(My.Settings.boolean1)
End Sub

Public Shared Function DoSomething(aBoolean As Boolean)
    Return Not aBoolean
End Sub 
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
0

The first thing you want to do is not reference the "My.Settings' directly. It's like referencing controls text/integer (or whatever) directly. Load the values and then manipulate them, and do what you need to do.

For this example have the two booleans as a likewise variable and load them in during initialisation in a Form_Load or initialisation routine.

private/public MySavedBoolean1 as Boolean
private/public MySavedBoolean2 as Boolean

Form_Load (or whatever)

MySavedBoolean1 = My.Settings.boolean1
MySavedBoolean2 = My.Settings.boolean2

Usage:

Private Sub Button1_Click() Handles Button1.Click
MySavedBoolean1 = Not MySavedBoolean1
'You can re-save it to settings if need be:
My.Settings.boolean1 = MySavedBoolean1
My.Settings.Save '(or you could do this later)
End Sub
video.baba
  • 817
  • 2
  • 6
  • 11