Solved

how to retain a static class across forms

Posted on 2011-02-14
11
317 Views
Last Modified: 2013-12-27
I am developing a windows mobile application with 5 input forms. As the data is entered into the form, i am writing it to a static class. After all 5 forms are entered, I save the data to the database. However, when I close form1 to load form2, the static class is getting reinitialized. How can I carry forward the same class, so the data is persistent. I tried it in a winform application and it worked but in the net CF application something is different, the class is getting destroyed everytime I call form.close event.
0
Comment
Question by:tornadog
  • 5
  • 4
  • 2
11 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 34889386
how and where do you declare it?
0
 
LVL 16

Expert Comment

by:hjgode
ID: 34890564
Declare the class var outside the form code, for example in program.cs

namespace TestStatic
{
      class MainClass
      {
            public class staticDataClass{
                  public static int iData = 0;
                  public static string sData = "";
            }
            public staticDataClass staticData = new staticDataClass();
            public static void Main (string[] args)
            {
                  MainClass.staticDataClass.sData="start";
                  WinForm form = new WinForm ();
                  Application.Run (form);
                  //Console.WriteLine("Hello World!");
            }
      }
      public class WinForm : Form
      {
            public WinForm ()
            {
                  InitializeComponent ();
                  
                  TestStatic.MainClass.staticDataClass.sData ="new value";
            }
...

Does this meet your need?

~Josef
0
 

Author Comment

by:tornadog
ID: 34890953
I want to keep the class static, so I dont have to instantiate it with each form load.. So I declared the class

as

public static class TestClass{
public static string data1 {get; set;}
public static string data2 {get; set;}
public static string data3 {get; set;}
}

And in my form1 , I have a Fill method that saves all the textbox data into TestClass as;

TestClass.data1 = TextBox1.Text;
and so on

Then form1 is closed on button click and in form2, something like
TestClass.data2 = TextBox1.Text;
lbl1.Text = TestClass.data1;  //data passed from form1 but I get a null reference exception here.

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:alexey_gusev
ID: 34891111
what happens if you remove "static" in the class definition? this way you get the same functionality but framework doesn't try to control when it loads it
0
 
LVL 16

Expert Comment

by:hjgode
ID: 34891840
Do you pass the static class by reference from form1 to form2? Otherwise you get a new class instance.

change the constructor of form2 to accept your static class as argument. Soemthing like this:

public class Form2{
     private TestClass myTestClass;
     Form2(ref Testclass testClass){
         this.myTestClass = testClass;
     ...
anywhere else, where you call Form2 include a reference of the TestClass
...
    Form2 _form2 = new Form2(ref myFirstTestClass);

But I would go with my first tipp and declare TestClass once globally ouside any Form code before the first form is instanciated.

0
 

Author Comment

by:tornadog
ID: 34892389
but if its a static class I cannot create a new instance like you do. Is that right?

for eg:

private TestClass myTestClass will be invalid if MyTestClass is a static class.
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 34892432
why should you??? MyTestClass.data1 is available from anywhere MyTestClass is visible from
0
 

Author Comment

by:tornadog
ID: 34892600
thats what I thought, but I am getting an empty string when I try to access data1 on form2. This whole thing works great in a simple winform application, but in a windows mobile application, it does not. The data is getting reset when I close the form.
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 34892621
no no, I mean without word "static" in the class definition
0
 

Author Comment

by:tornadog
ID: 34892820
yes I tried that too. Only the members are defined as static, the class itself is public.
0
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 500 total points
ID: 34892872
that is true, but you can keep it 'sealed' if you define private constructor.

I reckon it does what you need, apart from being non-static in a sense?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to setup the internet connectivity on Windows Mobile Emulator.   I assume that you already have Microsoft Visual Studio, Microsoft Windows Mobile SDK's and the emulator installed. The emulator is available with Visual Studi…
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question