0

I'm creating Library app using WinForms. I have eLibraryClasses where I have all data including each form services, and eLibraryUI where I have all my WinForms.

I have a problem in one form where I would like to change states of button.Visible to false or true.

I tried to extract method from UI to Service like:

public void ShowDrawnBook(bool clickedButtonVisible, bool toReadButtonVisible, int buttonNumber)
    {
        //Hide button which cover answer
        clickedButtonVisible = false;
        //Add option to add randomized book to "To read" bookshelf
        toReadButtonVisible = true;
        //Return index of clicked button
        buttonClicked = buttonNumber;
    }

And UI looks like for example:

service.ShowDrawnBook(randomBook2Button.Visible, toReadButton.Visible, 2);

I tried, but I couldn't use "ref" neither "out" for this properties. And in this way above it's building properly, but not working because of not changing parameters out of method. I have such a many of them in this form, so I could do it like

randomBook2Button.Visible = SomeMethod();
toReadButton.Visible = SomeMethod();
... for every variable

But I would like to avoid it.

Is there any way to send there buttons properties (bools) as parameters?

DerStarkeBaer
  • 669
  • 8
  • 28
  • https://stackoverflow.com/questions/4518956/a-property-or-indexer-may-not-be-passed-as-an-out-or-ref-parameter – Hans Passant Feb 20 '20 at 13:41
  • If the intent of `ShowDrawnBook` is to only modify the `Visible` property of the two source controls, then why not just pass the control references and then access their respective `Visible` properties? – TnTinMn Feb 20 '20 at 14:32

1 Answers1

1

Booleans are passed by value, not reference, thus your "problem".

To solve your problem, just take the Button(s) as parameters instead of the booleans. Button is a class, thus is passed by reference.

Then in your method change the state of the Button(s) properties as you see fit.

public void MyMethod(Button myButton1, Button myButton2) { myButton1.Visible = true; myButton2.Visible = false; }

Matteo Mosca
  • 7,380
  • 4
  • 44
  • 80
  • The problem was, that I couldn't use "Button" outside od winform classes. I tried to export method to another project where I have only models and services, and "Buttons" can't be use there – Krzysztof Aniśkiewicz Feb 21 '20 at 11:49
  • If your service library provides utility methods for winforms controls ther is nothing wrong to reference the appropriate assembly so "Button" becomes available. – Matteo Mosca Mar 04 '20 at 09:27