?
Solved

how to retain a static class across forms

Posted on 2011-02-14
11
Medium Priority
?
322 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
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

 
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

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.

Question has a verified solution.

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

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 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