-1

In my project, I'm dynamically generating a lot of elements on a form, like buttons, picture boxes etc.

To separate the code, I'll be making new classes. I've made an instance of form1 in my new class, but all the elements are inaccessible -- why is this?

Edit: below is the relevant code. I'm trying to create buttons in CreateParty, but when I reference an element in form1 (in this case gameScrollBar), I get an error

Inaccesible due to its protection level

My code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        CreateParty createparty = new CreateParty(this);
        generateIcons();
    }
}

public class CreateParty
{
    private Form1 mainForm;

    public CreateParty(Form1 form1)
    {
        mainForm = form1;
        testVoid();
    }

    public void testVoid()
    {
        Button NAButton = new Button();
        NAButton.Height = 100;
        NAButton.Width = 100;
        NAButton.Location = new Point(190, 100);
        NAButton.Text = "NA";
        mainForm.gameScrollBar.Controls.Add(NAButton);
    }
}

Any insight would be greatly appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Kindly put your code in your question as text, not as a screenshot. What does "mineaswell" mean? Your what is as well as what? Do you mean "as good"? I can't parse it either way. – 15ee8f99-57ff-4f92-890c-b56153 Oct 30 '17 at 15:56
  • 1
    Firstly - it is better that you add code to your question rather than a screenshot - if we get time to try & reproduce your problem we can simply cut & paste - it is less likely people will copy code from a picture. Just looking at that screenshot though you are assigning objects to looking variables in the constructor - you need to create public class members. – PaulF Oct 30 '17 at 15:57
  • I updated it with the relevant code. – Lawrence Banks Oct 30 '17 at 16:04
  • The error message gives a clue : "inaccesible due to it's protection level" - you need to make the elements accessible by making them public (or possibly internal if defined in the same assembly). In my original comment "looking variables" should have read "local variables". – PaulF Oct 30 '17 at 16:12
  • Just remove `mainForm` and use `gameScrollBar.Controls.Add(NAButton);`. You are trying to access `gameScrollBar` as if it was a static field. – Icemanind Oct 30 '17 at 16:23
  • Change the Modifiers property of any control you need to access in another class from Private to Internal. – Hans Passant Oct 30 '17 at 16:55
  • When you have a compiler error, make sure to include the exact message text as well as the line the error occurs on (the current answer is making an assumption on where the error occurs). – crashmstr Oct 30 '17 at 16:59
  • See marked duplicate for the answer to your literal question. But the real answer is **don't do that**. You should not be exposing the internals of your form to other classes. You should be wrapping important controls, passing through to their relevant properties, so that other types can affect only those properties and not the entire control. Even better, implement a decoupled approach where other types don't even need to access those things directly (e.g. let the form subscribe to events in those types, and let _it_ do the appropriate modifications). – Peter Duniho Oct 30 '17 at 23:20

1 Answers1

-1

You have declared the Form1 variable in your CreateParty class as private. Update your code to reflect:

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    CreateParty createparty = new CreateParty(this);
    generateIcons();
}


public class CreateParty
{
public Form1 mainForm;  //This line was updated

public CreateParty(Form1 form1)
{
    mainForm = form1;
    testVoid();
}

public void testVoid()
{
    Button NAButton = new Button();
    NAButton.Height = 100;
    NAButton.Width = 100;
    NAButton.Location = new Point(190, 100);
    NAButton.Text = "NA";
    mainForm.gameScrollBar.Controls.Add(NAButton);
}
objectively C
  • 960
  • 9
  • 25
  • This assumes the stated error occurs from `Form1` (or some other outside component) accessing `CreateParty`'s `mainForm`, but it seems more likely (from `testVoid`) that it is from `mainForm.gameScrollBar`, thus your suggestion is not going to fix the issue. – crashmstr Oct 30 '17 at 16:55
  • @crashmstr you clearly have not read the question. "but when I reference an element in form1 (in this case gameScrollBar), I get an error" Mainform was declared private and therefore would cause the protection level error. – objectively C Oct 30 '17 at 16:59
  • Yes, but the only shown usage of `mainForm` is in the class of which it belongs, and thus `private` is no problem. In other words, `mainForm.gameScrollBar` is a reference of an element of `Form1` in `CreateParty` and most likely where the error occurs. Thus `mainForm` is fine to be private, but the `.gameScrollBar` is the error. – crashmstr Oct 30 '17 at 17:01