Welcome to Dream.In.Code
Getting C# Help is Easy!

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!



How to access class array from another form

2 Pages V  1 2 >  
Reply to this topicStart new topic

How to access class array from another form

Junije
post 8 Jul, 2008 - 09:39 AM
Post #1


New D.I.C Head

*
Joined: 8 Jul, 2008
Posts: 13

Hi, I'm just switching from VB 6.0 to C# and have some problems with the transition.

My project has 2 forms, (Form1, and Form2), and 1 Class file(Person.cs).

In my main form, Form1, I've declared

CODE


public partial  class Form1 : Form
    {

// This is my user array ( from the class Person.cs)
Person[] User = new Person[10];

...

     public void Form1_Load(object sender, EventArgs e)
        {

            User[0] = new Person();
            User[0].Name = "Mike";
            User[0].Age = 21;
            User[0]....



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 sad.gif

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 =).






User is offlineProfile CardPM

Go to the top of the page


baavgai
post 8 Jul, 2008 - 10:09 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,581



Thanked 44 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(Junije @ 8 Jul, 2008 - 12:39 PM) *

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.

Hope this helps.
User is offlineProfile CardPM

Go to the top of the page

zakary
post 8 Jul, 2008 - 10:11 AM
Post #3


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 372



Thanked 4 times

Dream Kudos: 175
My Contributions


can you post the code for form2
User is offlineProfile CardPM

Go to the top of the page

Junije
post 8 Jul, 2008 - 10:42 AM
Post #4


New D.I.C Head

*
Joined: 8 Jul, 2008
Posts: 13

Hey, thanks for the answer bavgaii.

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...).

User is offlineProfile CardPM

Go to the top of the page

Junije
post 8 Jul, 2008 - 01:30 PM
Post #5


New D.I.C Head

*
Joined: 8 Jul, 2008
Posts: 13

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 sad.gif

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 smile.gif.

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;


        private void Form2_Load(object sender, EventArgs e)
        {


            form1Instance.ShowDialog();
            

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

         // Just trying to change the property as a test if it works, but it doesn't :(
            
         User[0].Age = 34;

          
        }
    }
}






User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 8 Jul, 2008 - 01:39 PM
Post #6


D.I.C Head

**
Joined: 21 Mar, 2008
Posts: 110



Thanked 6 times
My Contributions


add this to Form2 above the constructor...

csharp

public Person[] User;


then add this to Form1 where you want Form2 to open...

csharp

Form2 form2Instance = new Form2();
form2Instance.User = User;
form2Instance.ShowDialog();


then remove these lines from Form2....

csharp

Form1 form1Instance = new Form1();
Person[] User = form1Instance.User;


then remove this line from the Form2 Load Event...
csharp

form1Instance.ShowDialog();


This post has been edited by eclipsed4utoo: 8 Jul, 2008 - 01:44 PM
User is offlineProfile CardPM

Go to the top of the page

Junije
post 8 Jul, 2008 - 02:18 PM
Post #7


New D.I.C Head

*
Joined: 8 Jul, 2008
Posts: 13

Aarrghh.....I'm getting the same error as before :S

I'm new to these "classes", maybe I should have gone with the structs to build the array.

Thanks for the help everyone. biggrin.gif

I'll try and mess around with code, and read some stuff , maybe I'll solve it...

User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 8 Jul, 2008 - 06:06 PM
Post #8


D.I.C Head

**
Joined: 21 Mar, 2008
Posts: 110



Thanked 6 times
My Contributions


can you post the code that you have now?
User is offlineProfile CardPM

Go to the top of the page

Junije
post 9 Jul, 2008 - 04:53 AM
Post #9


New D.I.C Head

*
Joined: 8 Jul, 2008
Posts: 13

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'    

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            User[0] = new Person();
            User[0].Name = "Mike";
            User[0].Age = 21;
            User[0].Gender = 0;

        }

        private void button1_Click(object sender, EventArgs e)
        {
                Form2 form2Instance = new Form2();  
                form2Instance.User = User;  
                form2Instance.ShowDialog();  
        }
    }
}



Form2:

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 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'    




        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            listBox1.Items.Add("User[0].Name = " + User[0].Name);

        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}




This problem is a real pain in the ass... biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 9 Jul, 2008 - 05:08 AM
Post #10


D.I.C Head

**
Joined: 21 Mar, 2008
Posts: 110



Thanked 6 times
My Contributions


you need to make the Person[] array on Form2 public.
User is offlineProfile CardPM

Go to the top of the page

Junije
post 9 Jul, 2008 - 05:53 AM
Post #11


New D.I.C Head

*
Joined: 8 Jul, 2008
Posts: 13

Unfortunatly making the Person[] array public doesn't work, I get the error as stated in the code above ( commented section).

Is there any other way I can make the array available to the whole project, not just to the Form it was declared in?

I don't know why things have to be so complicated, and why the array isn't available to the other Forms in the first place.

Would the array from a parent form be accessible from a child form, i guess this could offer one solution to my problem?
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 9 Jul, 2008 - 06:05 AM
Post #12


D.I.C Head

**
Joined: 21 Mar, 2008
Posts: 110



Thanked 6 times
My Contributions


here is sample code on one way to pass variables between forms:

Form1...

csharp

public partial class Form1 : Form
{
private string[] array = null;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
array[0] = "test";

Form2 form2 = new Form2();
form2.secondArray = array;
form2.ShowDialog();
}

private void Form1_Load(object sender, EventArgs e)
{
array = new string[1];
}
}


Form2:

csharp

public partial class Form2 : Form
{
public string[] secondArray = null;

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
lblBefore.Text = secondArray[0].ToString();

secondArray[0] = "test2";

lblAfter.Text = secondArray[0].ToString();
}
}


"lblBefore" on Form2 shows "test", "lblAfter" on Form2 shows "test2".

QUOTE(Junije @ 9 Jul, 2008 - 05:53 AM) *

Unfortunatly making the Person[] array public doesn't work, I get the error as stated in the code above ( commented section).

Is there any other way I can make the array available to the whole project, not just to the Form it was declared in?

I don't know why things have to be so complicated, and why the array isn't available to the other Forms in the first place.

Would the array from a parent form be accessible from a child form, i guess this could offer one solution to my problem?


there are multiple ways of passing parameters. Using properties/public variables(like the example above) or passing the data through the constructor.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 9/8/08 12:34AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month