简体   繁体   中英

Read and Write into IList from another Class

I made a Class where my IList is located:

public class ListTable
{
    private IList<object> rolelist = new List<object>();

    public IList<object> GetList()
    {
        return rolelist;
    }
}

Then I added this to my MainWindow and add some stuff into the List: MainWindow.xaml.cs

ListTable rolelist = new ListTable();
private void CheckRights()
{
    IList<object> testlist = myClass.GetList();

    testlist.Add(new object[] { i,                          // 0
    (string)reader["role"],                             // 1
    (int)reader["can_add_kunde"],                       // 2
    (int)reader["can_add_assignment_kunde"],            // 3
    (int)reader["can_delete_kunde"],                    // 4
    (int)reader["can_edit_kunde"],                      // 5
    (int)reader["can_add_firmenkunde"],                 // 6
    (int)reader["can_add_assignment_firmenkunde"],      // 7
    (int)reader["can_delete_firmenkunde"],              // 8
    (int)reader["can_edit_firmenkunde"],                // 9
    (int)reader["can_add_assignment"],                  // 10
    (int)reader["can_delete_assignment"],               // 11
    (int)reader["can_edit_assignment"],                 // 12
    (int)reader["can_add_employee"],                    // 13
    (int)reader["can_delete_employee"],                 // 14
    (int)reader["can_edit_employee"],                   // 15
    (int)reader["can_see_adminpanel"],                  // 16
    (int)reader["can_change_columns_kunden"],           // 17
    (int)reader["can_change_columns_firmenkunden"],     // 18
    (int)reader["can_change_databaseTables"]            // 19
    });
}

now I want to access to this list from another class: FirmCustomer.xaml.cs

private ListTable list;
private void CheckRights(string username)
{
    IList<object> called = list.GetList();
    foreach (object[] item in called)
    {
        MessageBox.Show(item[1].ToString());
    }
}

but called is always null so it doesn't get the List.

Can someone tell my why it dont get the list? And how to get access to this list whith it's items?

Based on the code samples you provided, MainWindow and FirmCustomer are using two different instances of the ListTable class. You have added the items to the instance in MainWindow , but the instance in FirmCustomer doesn't know anything about the original instance.

When you create an instance of FirmCustomer , you should pass the existing instance of ListTable to it so that it can read out the values added previously.

Also, when adding the items to the list, the code is probably not doing what you intend it to do. The code you posted will add one object[] to the list. You should call IList.Add() for each item individually instead of adding them to the array first.

If you want to share the list between different classes you could make it static :

public class ListTable
{
    private static IList<object> rolelist = new List<object>();

    public static IList<object> GetList()
    {
        return rolelist;
    }
}

Usage:

IList<object> = ListTable.GetList();

As of now you are creating a new IList<object> for each ListTable instance that you create.

declare the field with static keyword it's common for all so, you can get the value add one class get another class. i was made one sample the code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReadIList
{
    class Program
    {
        static void Main(string[] args)
        {
            mainclass mc = new mainclass();
            mc.CheckRights();
            secondclass sc = new secondclass();
            sc.CheckRights(string.Empty);
        }
    }
    public class ListTable
    {
        public static  IList<object> rolelist = new List<object>();

        public IList<object> GetList()
        {
            return rolelist;
        }
    }

    public class mainclass
    {
        ListTable rolelist = new ListTable();
        private ListTable myClass= new ListTable();
        public void CheckRights()
        {
            IList<object> testlist = myClass.GetList();
            testlist.Add(new object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 });   
        }
    }

    public class secondclass
    {
        private ListTable list=new ListTable();
        public void CheckRights(string username)
        {
            IList<object> called = list.GetList();
            foreach (object[] item in called)
            {

                Console.WriteLine(item[1].ToString());
            }
            Console.ReadLine();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM