0

Say I have the following class:

public class ListArticle
    {
        public List<string> Clothes
        {
            get
            {
                return _clothes;
            }
            set
            {
                if (value != _clothes) _clothes = value;
            }
        }

        public List<string> Colors
        {
            get
            {
                return _colors;
            }
            set
            {
                if (value != _colors) _colors = value;
            }
        }

        private List<string> _clothes { get; set; }
        private List<string> _colors { get; set; }
    }

I want to add strings to my lists into, let's say, form1 and use them in another form such as form2. How is this possible?

C. Cristi
  • 569
  • 1
  • 7
  • 21
  • What is the relationship between the two forms? If Form1 opens Form2 then you can simply pass the instance of your ListArticle built in Form1 to Form2 – Steve Dec 27 '16 at 19:40
  • show us how you tried passing the info between forms. there are at least 2 ways i can think of off the top of my head – Muckeypuck Dec 27 '16 at 19:40
  • Pass the instance of `ListArticle` from form1 to form2 – rageit Dec 27 '16 at 19:41
  • [Interaction between forms — How to change a control of a form from another form?](https://stackoverflow.com/questions/38768737/interaction-between-forms-how-to-change-a-control-of-a-form-from-another-form) – Reza Aghaei Dec 27 '16 at 19:49
  • Seems a bit pointless to bother checking `value` only to stop reassigning the same value to the property, given there's no side effect of doing so. – 404 Dec 27 '16 at 19:53
  • I tried to declare a variable like this in form1: `ListArticle Names = new ListArticle();` but when I try to use properties like `Names.Clothes.Add();` it doesn't work.. – C. Cristi Dec 27 '16 at 19:54
  • "it doesn't work" is not helpful. Are you getting an exception? Bad results? Are you creating a new object in `form2` and expecting it to have the same data as one created in `form1`? – D Stanley Dec 27 '16 at 19:56
  • @DStanley yeah I am creating a new object in form2 and expect to have the same data in form1 ... how can I do this? – C. Cristi Dec 27 '16 at 19:58
  • As others have suggested - pass the reference from `form1` to `form2`. You can do that via a constructor parameter, property of one of the forms, etc. There are many questions about passing data between forms. – D Stanley Dec 27 '16 at 19:59
  • @DStanley I have no idea how to do this.. the thing I know is when I try to add to the list I can't because it shows me to create an internal class but if I do this I dont think I can use the data in other forms too... – C. Cristi Dec 27 '16 at 20:03
  • Here's a start: http://stackoverflow.com/search?q=%5Bc%23%5D+pass+data+between+forms – D Stanley Dec 27 '16 at 20:04

2 Answers2

1

Addressing the issue in your comment

I tried to declare a variable like this in form1: ListArticle Names = new ListArticle(); but when I try to use properties like Names.Clothes.Add(); it doesn't work..

Depends what you mean by "it doesn't work", but if that's really all you did it sounds like you haven't actually created your Lists yet.

So change class to this and try again:

public class ListArticle
{
    public List<string> Clothes { get; private set; }
    public List<string> Colors { get; private set; }

    public ListArticle()
    {
        Clothes = new List<string>();
        Colors = new List<string>();
    }
}

(edit)

Doing the above will allow you to do something like Names.Clothes.Add() without an exception being thrown (which was how I interpreted your comment).

Now let's see how you can get the same data from Form1 into Form2:

Form1.cs

public class Form1 : Form
{
    private readonly ListArticle _articles = new ListArticle();

    // assume strings have been added to articles by this point.

    // option 1 - use same ListArticle instance
    public void Foo()
    {
        var form = new Form2();
        form.Articles = _articles; 
    }

    // option 2 - add strings to new ListArticle
    public void Bar()
    {
        var articles = new ListArticle();
        articles.Clothes.AddRange(_articles.Clothes);
        var form = new Form2();
        form.Articles = articles;
    }
}

public class Form2 : Form
{
    public ListArticle Articles { get; set; }
}

I must stress of course that these are not the only ways and certainly not the best ways to do it - just a couple simple examples to hopefully accomplish what you want. If this still doesn't work then you will have to be much clearer about what end result you expect, and how exactly you're trying to accomplish it.

404
  • 8,022
  • 2
  • 27
  • 47
  • And that's it? I can use data I add into from2 in form1? – C. Cristi Dec 27 '16 at 21:03
  • What if I want to use form2 data in form1? – C. Cristi Dec 28 '16 at 13:40
  • @C.Cristi There are many ways to share data between classes: the code that creates both could supply each with the same data; each class could have a property or method exposing the data which the other class can access; the data might be stored and created by some other class elsewhere, and both form classes access the data from that same location; etc... – 404 Dec 28 '16 at 14:05
  • ...I tried your option 2 method but It shows me that list is null ... as I said I want something that I can use in `form1` **FROM** `form2` .... and If I try to press my button which shows the string in the list to catch an exception such as "the list is null" or whatever.. – C. Cristi Dec 28 '16 at 15:46
0

You can make the constructor of Form2 accept a ListArticle object. Also if you will only have one ListArticle across your system then you can make the class and all of its properties as Static.

NicoRiff
  • 4,803
  • 3
  • 25
  • 54