Join 109,581 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,526 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
From the same form I can easily access all the user arrays and change the properties. But when I try to access them from the other form, for some reason It doesn't work
In Form2, I want to change certain User[0] properties when clicking on a button, but the Code view in Form2 doesn't recognize the User[] array.
This is probably a very simple problem, but I'm a newbie and I don't know much, so I hope someone can help me =).
This is probably a very simple problem, but I'm a newbie and I don't know much, so I hope someone can help me =).
That depends, actually.
First, try making the array public, like this:
csharp
public Person[] User = new Person[10];
Now, if your form2 is doing something like this, you're fine.
csharp
Form1 form1Instance = new Form1(); form1Instance.ShowDialog(); Person[] User = form1Instance.User;
However, if you want to access the thing globally and have an instance, you have a host of issues.
I'm going to guess that Form2 might be a dialog for modify a Person object? If so, consider something like this.
csharp
public partial class Form2 : Form { // my reference to a person object private Person person;
// how other objects access my person object public Person CurrentPerson { get { return this.person; } set { this.person = value; } } }
public partial class Form1 : Form { private void EditPerson(Person person) { Form2 frm = new Form2(); frm.CurrentPerson = person; frm.ShowDialog(); }
public void Button3_Click(object sender, EventArgs e) { EditPerson(this.User[2]); } }
If this is confusing, please read up on classes and object / instances. Completely understand what static means and the difference between an object and a class method. Trading values around between objects is a very common confusion. Unfortunately, there are a number of ways to approach the problem, so there's no single correct answer.
I've tried making the array public but it still doesn't get recognized in the Form2 when i type User[0].
I'll try the other things you wrote...
As for code in the Form2, it's really not much important I guess, but I have a button, and a listbox. What I'm trying to do is when I open the Form2 (from the Form1 interface), and then click on the button, the listbox would then show all the User[] information( Name, Age...).
I've tried this and it seems you're on the right way.
CODE
1. Form1 form1Instance = new Form1(); 2. form1Instance.ShowDialog(); 3. Person[] User = form1Instance.User;
The User[] Array gets recognized while typing but when building the application I get an error
The error stands:
CODE
Error 1 The type 'WindowsFormsApplication1.Form1' already contains a definition for 'User'
And it highlights this part of the code in the Form1(which i didn't write, I guess it appeared when I declared the array):
CODE
internal Person[] User() { throw new NotImplementedException(); }
This is my whole form2 code, maybe I just need to place the code differently or modify something to make the error disappear, I don't have much experience in C# so the tiny problems that appear out of nowhere really bug me sometimes .
CODE
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form2 : Form {
public Form2() { InitializeComponent();
}
Form1 form1Instance = new Form1(); Person[] User = form1Instance.User;
I've done everything you wrote, but I'm getting an error:
CODE
Error 1 Form2.User' is inaccessible due to its protection level
Highlighting this part of the code:
CODE
form2Instance.User = User;
The full code of my project is shown here:
Form1:
CODE
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace Project { public partial class Form1 : Form {
Person[] User = new Person[10]; // If I make this above public, I get a different error: // Error 1 Inconsistent accessibility: field type 'Project.Person[]' is less accessible than field 'Project.Form1.User'
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace Project { public partial class Form2 : Form {
Person[] User; // If I make this above public, I get a different error: // Error 1 Inconsistent accessibility: field type 'Project.Person[]' is less accessible than field 'Project.Form2.User'