Solved

how to retain a static class across forms

Posted on 2011-02-14
11
316 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exchange 2013 doesn´t allow mobil phones to connect (iphone and android) 4 116
ActionBar problem 7 144
cheap and best smart phone 19 94
Contraction monitoring App 2 75
I think the most popular question in our Windows Mobile Programming zone is about the backlight - we all want to keep it on when our application is running. Few years ago the function SystemIdleTimerReset() did this job. On our side we had to detec…
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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