-3
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    class mylist
    {
        public List<string> list1 = new List<string>();
    }

    class mylist2
    {
        public List<string> list2 = new List<string> { "aton", "electron" };
        public void listconvert()
        {
            mylist ml = new mylist();
            ml.list1.AddRange(list2);
            MessageBox.Show("do you kn ow");
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mylist2 ml2 = new mylist2();
        ml2.listconvert();
        mylist ml = new mylist();

        for (int i = 0; i < ml.list1.Count; i++)
        {
            textBox1.AppendText(ml.list1[i].ToString() + "\n");
        }
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Raja
  • 3
  • 5
  • 1
    Welcome to Stack Overflow! What exactly are you trying to do and what prevents that from working? Please edit that into the body of your question. (It would also help to fix the indentation so the code shows up coherently.) – Nathan Tuggy Jan 29 '15 at 02:43

2 Answers2

0

I'll narrow it down to these lines:

        mylist2 ml2 = new mylist2();
        ml2.listconvert();
        mylist ml = new mylist();

The third line in that snippet is working with a different instance of your mylist type than the second line in that snippet. When you then go on to interator over the ml variable, you haven't done anything with that list yet.

Maybe you want this:

public class mylist
{
    public List<string> list1 = new List<string>();
}

public class mylist2
{
    public List<string> list2 = new List<string> { "aton", "electron" };

    public void listconvert(List<string> other)
    {
        other.AddRange(list2);
        //MessageBox.Show("do you kn ow");
    }
}

private void button1_Click(object sender, EventArgs e)
{
    mylist ml = new mylist();
    mylist2 ml2 = new mylist2();

    ml2.listconvert(ml);   
    for (int i = 0; i < ml.list1.Count; i++)
    {
        textBox1.AppendText(ml.list1[i].ToString() + "\n");
    }
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Hello Nathan & Joel. I am trying to write a script to process a data from analytical instruments. What I posted is a simple version of that. I am trying to exchange data between the list variables (from list2 to list1) using a button. My problem is - this script is not either giving a error or giving result. – Raja Jan 29 '15 at 03:46
  • Yeah Joel. Exactly! Thank you. I was't having clarity in effectively returning data from the methods in one class to the corresponding variables in other classes. Its very clear now. Thank you. I will try and get back to you by evening. Thank you. – Raja Jan 29 '15 at 07:11
0

Your appear to have two issues.

First, this code creates a list but doesn't store a reference anywhere so it is effectively lost:

    public void listconvert()
    {
        mylist ml = new mylist();
        ml.list1.AddRange(list2);
        MessageBox.Show("do you kn ow");
    }

Second, this code creates an empty list and then tries to add the elements of this empty list to the text box:

        mylist ml = new mylist();

        for (int i = 0; i < ml.list1.Count; i++)
        {
            textBox1.AppendText(ml.list1[i].ToString() + "\n");
        }

You probably want to change your code to be like this:

    class mylist2
    {
        public List<string> list2 = new List<string> { "aton", "electron" };
        public mylist listconvert()
        {
            mylist ml = new mylist();
            ml.list1.AddRange(list2);
            MessageBox.Show("do you kn ow");
            return ml;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mylist2 ml2 = new mylist2();
        mylist ml = ml2.listconvert();

        for (int i = 0; i < ml.list1.Count; i++)
        {
            textBox1.AppendText(ml.list1[i].ToString() + "\n");
        }
    }
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Thank you for your quick response Enigmativity. I will try that and get back to you. I was assuming 'mylist ml = new mylist();' is just creating object to the class and holding variables - 'list1' with same data, does not matter how many times I call class with 'mylist ml = new mylist();'. – Raja Jan 29 '15 at 04:05
  • @RajaSekharV - What do you mean by "does not matter how many times I call class with `mylist ml = new mylist();`". Oh, and to inline code you use a back-tick (under the `~`) and not a single-quote `'`. – Enigmativity Jan 29 '15 at 05:57
  • Oops! I meant to seek help Enigmativity. By mistake I have copied code in the proposal section. Sorry. I don't know how to take it back. May be it possible by disagree of the proposal from your side. – Raja Feb 01 '15 at 15:01
  • I would like to share flow chart and images and discuss about it. Is it possible to do here – Raja Feb 02 '15 at 06:56
  • @Raja - You can post a new question (and include images) or you can create a chat room and discuss in there if it's not a strict question. – Enigmativity Feb 02 '15 at 08:13
  • @ Enigmativity - I have posted as new question. "Assigned values to a variable in a class from Button click event outside. But returning zero as output from another button click". If it is not helping I will add more information in the form of flow charts. Please help. – Raja Feb 02 '15 at 09:01
  • @Raja - You can also paste links into comments. Much easier to find a question from its link. – Enigmativity Feb 02 '15 at 10:24