1

i have a main form where i can set settings and show some stuff inlcuding a "MenuStrip" and a second form that opens upon clicking "Add" Button in Form 1.

In The 2. Form the user can add multiple strings to text boxes. After clicking "Done" in Form 2 all the Informationes will be saved in an multidimensional array [,].

The form 1 then retrieves these Informations with get/set.

The Problem: I am getting an Infinite Loop on the get methode =/ I think it is because i have not defined how many columns/rows the array has (which is impossible since the user can add more informations dynamcally). So maybe i have to pass the reference (and how do i manage that? i know how to pass things between forms when CREATING an instance of a new form but not when the old form is already open and i close the 2nd one) or is there any other way?

My Code:

    //in Form 1
    private void btn_Add_Click(object sender, EventArgs e)
    {
        using (form2 = new AddStuff())
        {
            form2.ShowDialog();
            string [,] copy = form2._NewMenuStrip.Clone() as string [,];
            for (int i = 0; i < copy.GetLength(0);i++ )
            {
                for (int j=0; j < copy.GetLength(1);j++)
                {
                    MessageBox.Show(copy[i, j]);
                }
            }
        }
     }

     //in Form 2
    public string [,] _NewMenuStrip 
    {
        get { return _NewMenuStrip; } // Here i get the infinite Loop
        set { _NewMenuStrip = value;}
    }

    private void btn_Done_Click(object sender, EventArgs e) 
    {
        WriteInformationToArray();         
        this.Close();   
    }

Thanks in Advance!

Regards, Christian

1
  • You need to have a backing field for the property _NewMenuStrip to use in the get {return}, you are self referencing. A way to do it implicitly is how Romano highlighted below Commented Jun 7, 2017 at 6:48

1 Answer 1

4

You get that infinite loop, because the property is self referencing. You need to either access a variable or just leave the getter and setter definitions empty. .Net will take care of the variables then. Just define it like this:

public string [,] _NewMenuStrip { get; set; }

OR

define a private variable holding your value:

private string [,] _newMenuStrip;

public string [,] _NewMenuStrip
{
    get { return _newMenuStrip; } //<--- Set the private one !
    set { _newMenuStrip = value; } //<--- return the private one !
}

which is the exact same thing like above ( the complier will create a private field for you) !

Sign up to request clarification or add additional context in comments.

4 Comments

Damn you are great! THANKS this works! one last question in Form 2 i write the information like this to the array: _NewMenuStrip[0,0] = tb_SomeTB.Text; _NewMenuStrip[1,0] = tb_AnotherTB.Text; and i get error "An unhandled exception of type 'System.NullReferenceException" You may know why? (completly new to c# and programming sorry) ill accept your answer once i can (2minutes) THANKS
U will get a NullReferenceExeption when ever u are doing some operations on objects that don't 'exist'. My first guess would be your array is not initialized correctly. If you Debug you can check your array and see if the items you are setting (0,0) and (1,0) even exist. What is the Lenght of your array ?
The problem is, that you tell the compiler to build a multidimensional Array, but not how the dimensions should be. Lets assume you need to store 10 times 2 values (Perhaps ten menuitems with their text and a shortcut or something). You would better use the second approach (with the additional variable) and define it as public string[10,2] _newMenustrip = new string[10,2];
THANKS! already thought something like that! Got it fully working now! Have a nice day and thank you! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.