-1

So I had this app made that worked in a console. It is very simple, I create a catalog in main class, and then engage it. From then on, all the actions happens in a class called "catalog", it reads the console from there, so all the variables are local in that class.

Now I was forced to recreate that app in Windows Forms. After a couple of hours learning how to use it, I faced a major problem. While I by now know how to read the variables I need, I noticed I cannot use them in a different class.

So far I have read a string, and an array of integers (I used ArrayList) in a form. Now I need to access those two variables (string name and ArrayList numbersUsed) in my catalog class, but it says they "do not exist in current context".

How can I use the variables read from forms in other classes? I am a complete beginner with C#, so sorry if this is a basic question.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Gytis39
  • 37
  • 7
  • 1
    pass it in its constructor – Vivek Nuna Oct 25 '16 at 21:11
  • Create a constructor for your new class that is passed the two variables as parameters. Or make the variables `public`, and instead pass a reference to your form object that contains these variables. – EMUEVIL Oct 25 '16 at 21:14

1 Answers1

1

Initialize new form like this and pass data to it in constructor.

ArrayList arr = new ArrayList();
string str ="";
Form2 form2 = new Form2(str, arr);

Constructor:

public Form2(string strvar, ArrayList arrList)
        {
            //Use strvar and arrList 
            InitializeComponent();
        }
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197