0

I'm writing a webcam app and I need to write and read about 15 variables. I have two forms, the main window and the options window. When I save my options I do something like this:

string[] lines = {x , y, ..., z  };

System.IO.File.WriteAllLines(@"config.cfg", lines);

In my main window I read the variables using the StreamReader function:

public void Foo()
{

    List<string> lines = new List<string>();

    while (!reader.EndOfStream)
    {
        lines.Add(reader.ReadLine());

        reader.Close();
    }

    x = Convert.ToInt32(lines[0]);
    y = Covert.ToString(lines[1]);
    // and so on...
}

The problem is I don't know how to access x and y in another method. Btw: I declared all my variables as public static. Can anyone please help?

Edit

It is a windows forms app, the two windows do not exist at the same time. Maybe someone can give me a hint how to store those variables using a different method? The only thing I know I could do is store the vars using a MySQL database, but doesn't make much sense.

I'll try to clarify: what I would like to do, is pass x and y from the method Foo to another method. I can't use global variables, because reading the variables from the file requires a method.

4
  • Where are you declaring x and y? why not declare string[] lines as public static?
    – Emmanuel N
    Jan 16, 2012 at 15:39
  • would setting x and y as viewstates help? or maybe your line array save in viewstate
    – Andres
    Jan 16, 2012 at 15:39
  • Well, I don't know how to start answering your question. You're doing it all wrong, yet it will be hard to explain to you...
    – Zruty
    Jan 16, 2012 at 15:40
  • It IS a webcam app. However, is this winform or web app? Jan 16, 2012 at 15:41

3 Answers 3

0

use properties=> setting to save your values if you will need it the next time. it's simple and help you to avoid some problems

1
  • Ok, found a tutorial on CodeProject and helped me a lot! Thanks! Jan 16, 2012 at 16:32
0

Do both forms exist at the same time? i.e in skype you have your main skype window and your chat windows?

If so just make a CameraUpdated event, which sends the lines changed to any subscribers, then that way you can just get your main window to bind to any new child windows events, so it will be notified.

To do this you would need to make a custom event arg to pass the data, a delegate for the event name and args, and expose the actual event on your form.

If however only one form exists at a time, you could either have an in memory store you push data to and then read it out of (much like your current implementation), or when you flip from one form to the other you pass in the data that has changed.

Ultimately I would try to avoid having global style data, be it in a file, memory or a global program variable, and opt for some loosely coupled approach, but without knowing your actual implementation its difficult to advise a specific solution.

1
  • The two forms may exist at the same time, but then the camera stream is off. The settings are applied when the stream is turned back on (the variables are supposed to hold fps values etc). If could show me another way of writing/reading variables I would be gratefull Jan 16, 2012 at 15:59
0

Class members that are declared as public static are accessible with:

ClassName.MemberName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.