C# 从不同类中的方法访问ArrayList

C# 从不同类中的方法访问ArrayList,c#,C#,我正在做我大学以外的第一个项目。 我有两节课。在第一个示例中,我有一个将字符串添加到arraylist的方法。 在第二个类中,我希望从上一个类中的方法访问arrayList,并获取其中的元素 我怎么能这么做? 感谢您的帮助。您可以在第一个类中将ArrayList作为静态属性公开,然后可以从第二个类访问该属性 public class First { public static ArrayList MyList { get; set; } } public class Second {

我正在做我大学以外的第一个项目。 我有两节课。在第一个示例中,我有一个将字符串添加到arraylist的方法。 在第二个类中,我希望从上一个类中的方法访问arrayList,并获取其中的元素

我怎么能这么做?
感谢您的帮助。

您可以在第一个类中将ArrayList作为静态属性公开,然后可以从第二个类访问该属性

public class First
{
    public static ArrayList MyList { get; set; }
}

public class Second
{
    public void SomeMethod()
    {
        //First.ArrayList will give you access to that class
    }
}

最好不要使用ArrayList(如果您使用的是.Net 2.0或更高版本),而是使用类型安全的属性。您可以在第一个类中将ArrayList作为静态属性公开,然后从第二个类访问该属性

public class First
{
    public static ArrayList MyList { get; set; }
}

public class Second
{
    public void SomeMethod()
    {
        //First.ArrayList will give you access to that class
    }
}

最好不要使用ArrayList(如果您使用的是.Net 2.0或更高版本),而是使用类型安全的

,除非您使用的是.Net 1.1,否则我会避免使用ArrayList,而使用它们的强类型对应项

您需要在类1中创建该方法。然后您可以从类2访问它,如果它是或您有类1的实例

例如:

public class Class1{
    public List<String> getList()
    {
        // create the list and return it
    }
}

public class Class2{
    Class1 firstClass{ get;set; }
    void foo()
    {
        // now you can access the List<String> of class1 via it's instance
        List<String> list = firstClass.getList();
        foreach(String s in list)
        {
            // do something
        }
    }
}
公共类1{
公共列表getList()
{
//创建列表并返回它
}
}
公共课2{
Class1第一类{get;set;}
void foo()
{
//现在您可以通过class1的实例访问它的列表
List List=firstClass.getList();
foreach(列表中的字符串s)
{
//做点什么
}
}
}

除非您使用的是.NET 1.1,否则我会避免使用
数组列表
,而使用它们的强类型对应项

您需要在类1中创建该方法。然后您可以从类2访问它,如果它是或您有类1的实例

例如:

public class Class1{
    public List<String> getList()
    {
        // create the list and return it
    }
}

public class Class2{
    Class1 firstClass{ get;set; }
    void foo()
    {
        // now you can access the List<String> of class1 via it's instance
        List<String> list = firstClass.getList();
        foreach(String s in list)
        {
            // do something
        }
    }
}
公共类1{
公共列表getList()
{
//创建列表并返回它
}
}
公共课2{
Class1第一类{get;set;}
void foo()
{
//现在您可以通过class1的实例访问它的列表
List List=firstClass.getList();
foreach(列表中的字符串s)
{
//做点什么
}
}
}

最好的选择是使用一个只读属性来公开arraylist,如下所示:

class MyClass
{
    private ArrayList FArrayList;
    public ArrayList ArrayList { get { return FArrayList; } }

    ...

最好的选择是使用一个只读属性来公开arraylist,如下所示:

class MyClass
{
    private ArrayList FArrayList;
    public ArrayList ArrayList { get { return FArrayList; } }

    ...
试试这个..

public class First
{
    public ArrayList MyList;
    public First()
    {
       MyList = new ArrayList();
    }
    public void AddString(string str)
    {
       MyList.Add(str);
    }
}
public class Second
{
    public void someMethod()
    {
       First f = new First();
       f.AddString("test1");
       f.AddString("test2");
       f.AddString("test3");
       ArrayList aL = f.MyList; // you will get updated array list object here.
    }
}
试试这个..

public class First
{
    public ArrayList MyList;
    public First()
    {
       MyList = new ArrayList();
    }
    public void AddString(string str)
    {
       MyList.Add(str);
    }
}
public class Second
{
    public void someMethod()
    {
       First f = new First();
       f.AddString("test1");
       f.AddString("test2");
       f.AddString("test3");
       ArrayList aL = f.MyList; // you will get updated array list object here.
    }
}