Can't access list <string> from another class

I am trying to access a list declared in the ImageCollection class. Here is the code.

class ImageCollection
{
    public List<string> imgCollection = new List<string>();       

    public void addImgCollection(string path)
    {
        imgCollection.Add(path);
    }

    public List<string> getList()
    {
        return imgCollection;
    }
}

      

And add image path to imgCollection from AutoClass using this code.

ImageCollection imgcoll = new ImageCollection();
imgcoll.addImgCollection(img1);
imgcoll.addImgCollection(img2);

      

But when I access the list from MainForm:

ImageCollection image = new ImageCollection();
List<string> imgcol = image.getList();

      

The result is empty. What's wrong with my code?

+3


source to share


2 answers


This is where you instantiate a new ImageCollection. This is why this imgCollection property has a default value (which null )

:

ImageCollection image = new ImageCollection();
List<string> imgcol = image.getList();

      

I believe what you tried to do:

public class AutoClass 
{
   public static ImageCollection imgcol1 = new ImageCollection();

   // somewhere in your AutoClass:
      imgcoll.addImgCollection(img1);
      imgcoll.addImgCollection(img2);   
}

      

Then you can do from your MainForm class:



// copies collection to imgcol (therefore creates a new collection with samve values)
List<string> imgcol = AutoClass.imgcol1.imgCollection.ToList(); 

// saves reference to ImgCollection collection to imgcol variable (not copying anything)
List<string> imgcol = AutoClass.imgcol1.imgCollection; 

      

If you don't want to use a static property, you can use the instance property instead:

public class AutoClass 
{
   public ImageCollection imgcol1 = new ImageCollection();

   // somewhere in your AutoClass:
      imgcoll.addImgCollection(img1);
      imgcoll.addImgCollection(img2);   
}

      

And in your MainForm:

public partial class MainForm : Form
{
    AutoClass ac = new AutoClass();

    ...

    void MyMethod
    {
        List<string> imgcol = ac.imgcol1.imgCollection; 
        // (you don't really need this GetList method of AutoClass at all)
    }
}

      

+1


source


The problem is with the first line:

ImageCollection image = new ImageCollection();
List<string> imgcol = image.getList();

      

A new instance of the ImageCollection class is created, so its imgCollection property becomes empty again.

In your AutoClass, you have to open an ImageCollection instance in your MainForm.

It will look more or less like



public class AutoClass
{
       public ImageCollection Imgcoll { get; set; }

       public AutoClass()
       {
              Imgcoll = new ImageCollection();
       }

       public void SomeMethod(someargs)
       {
              Imgcoll.addImgCollection(img1);
              Imgcoll.addImgCollection(img2);
       }
}

      

And then in MainForm:

//autoClassInstance should be somewhere initialized and defined within MainForm
List<string> imgcol = autoClassInstance.ImgColl.getList();

      

Also, check out Microsoft's Naming Guide - https://msdn.microsoft.com/en-us/library/ms229002(v=vs.110).aspx - because you are specifying instances and methods incorrectly.

0


source







All Articles