Solved

how to retain a static class across forms

Posted on 2011-02-14
11
314 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Progress means simplifying, not complicating. Bruno Munari Preface How to detect the name of the internal storage or an SD-card on Windows Mobile device from the desktop application? I got this question, when I was working on a PC applicati…
If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now