I looked through the archives and didn't really find an answer to my question.
In my Main form, I have a "global" array that holds some data. I want to be able to access and make changes to that array from a child window. I know that I can set up a "retrieval" method in the Main form to send the child form the array. I do that with the following code:
SunSystem[] s = ((frmMain)this.MdiParent).GetArray();
I would rather just modify and access the array located on the frmMain in case I have multiple child forms open at once. Is this possible
Thanks in advance,
Rob
rclint@NOSPAM.uwsp.edu
(remove NOSPAM to email me)

Access Main Form Variables From Child
davidc1110
Nirupam Pratap Reddy
James
maxithron
This is what I have so far (minus unnecesary stuff):
frmMain:
public class frmMain : System.Windows.Forms.Form
{
static SunSystem[] s;
private void FillArray()
{
Random r = new Random();
s = new SunSystem[r.Next(10,20)];
for (int z = 0; z<s.Length;z++)
{
s[z] = new SunSystem(r,z);
}
}
public void OpenChild(object sender, System.EventArgs e)
{
frmChild ss = new frmChild();
ss.MdiParent=this;
ss.Show();
}
}
BartonFinkle
Thats why it works!
Remember that the code "private SunSystem[] s;" creates two things: the "SunSystem[]" object in memory and a reference (or pointer) to that object called "s".
The child form is creating a new reference, also called "s", and pointing it to the existing object created on the main form via the main form's reference to that object.
Make sense
Greg Hurlman
I was able to get the code converted to c#. I figured it would be polite to reply in case some oens else wanted the c# code.
In the Main form I have the following code:
private SunSystem[] s;
public SunSystem[] SunSystemArray
{
get
{
return s;
}
set
{
s = value;
}
}
In the child form I have the following line:
SunSystem[] s = ((frmMain)this.MdiParent).SunSystemArray;
This succeeds in delivering the array to the child form and I can change the properties of the array. Those properties are saved to the array on the main form. I honestly am not sure why it works since it appears I am creating a new instance of the array on the child form but the changes made from the child form are saved to the main form.
Thanks again for all the help.
RobC
ClaraOscura
Declare a public property on the main form that gets/sets the private Array. Then all child windows can refer to This.MdiParent.ArrayProperty to access the array on the main form.
I'm not sure how properties are declared in C# so here's a quick VB example you can translate:
Public Class frmMain
Inherits System.Windows.Forms.Form
#Windows Designer Stuff#
Private myS() As SunSystem
Public Property SunSystemArray() As SunSystem()
Get
Return myS
End Get
Set(ByVal value As SunSystem())
myS = value
End Set
End Property
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Call Array Filling Code
End Sub
'Insert rest of form code
End Class
That will put a property on frmMain that allows access to the array. The child forms would then access this property using code something like:
CType(This.MdiParent, frmMain).SunSystemArray
Another method might be to reverse this logic, putting the array property on the child forms and setting its value equal to the main form's array when the child is created.
Hope that helps!