Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

how to retain a static class across forms

Posted on 2011-02-14
11
Medium Priority
?
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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