1
Answer

[C#] Access items from Generic List in another Form

Jay S

Jay S

10y
1.5k
1
Hi All..

Having some problems trying to do something relatively simple:

Form1:

Opens Form2.

Form2:

Contains some controls for the user to input (textboxes, drop downs etc)

I add these into a list:

namespace Foo
{
public System.Collections.Generic.List<AddPlayerClass> addPlayerList;
public partial class FrmAddPlayer : Form
    {
public FrmAddPlayer()
{
//
            InitializeComponent();
//
//
//
}
public void addPlayer()
        {
this.addPlayerList = new System.Collections.Generic.List<AddPlayerClass>();
//
//
//
addPlayerList.Add(new AddPlayerClass {  ID = peopleID,
                                                    UID = maxUID,
                                                    FirstNameID = Convert.ToInt16(cboPlayerFname.SelectedValue),
                                                    SecondNameID = Convert.ToInt16(cboPlayerSname.SelectedValue)
 });
//
//
//
}
}
All fine..

I use this class to populate the list:

namespace Foo
{
    public class AddPlayerClass
    {
        //PERSONS DETAILS
        public short ID { get; set;}
        public int UID { get; set;}
        public short FirstNameID {get; set;}
        public short SecondNameID {get; set;}
        public short CommonNameID {get; set;}
}
}

Again fine.

In From2 I am then calling a Method in Form1, this is done AFTER the list has populated. (list will always only have one item/object in it)

In Form1 I am trying to access this list so I have done:

Form2 f2 = (Form2)Application.OpenForms["Form2"];
List<AddPlayerClass> addDetails = f2.addPlayerList;

All fine.

When I then do this:

int test = addDetails.ID;

The intellisense can't fine ID as part of the addDetails.

I can't understand what I am doing wrong.

The list will only ever have one object/item in it, all I want to do have access to it.



Answers (1)