Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Static (IsShared) property keeps getting reset

Hi there,

I have a control that I want to use in several files.

I have therefore set it up as:

          public static System.Windows.Forms.Label label1;

All references to this in my main application I have changed from "this.label1" to "label1".  In the other classes that I want to use this in, I inherit the main application and use "label1".

This all works.

However Visual Studio (.Net 2003 C#) keeps on automatically (every now and then) changing this back to the defaults:

          public System.Windows.Forms.Label label1;     // NO STATIC

And it changes all "label1" to "this.label1"

Has anyone experienced this?  It's very annoying!

Hope you can help.

Thanks,

James

0
jamesnoe
Asked:
jamesnoe
4 Solutions
 
AlexFMCommented:
Move this member to some generic (non-Control) class which is not handled by the Form Designer.
0
 
jamesnoeAuthor Commented:
I have tried this - and it builds and runs OK - however I can no longer see the control in my application - this isn't really ideal - any ideas?

All I want to do is have a control that I can see, move around, edit etc... in the design view, and access it in multiple files without Visual Studio messing it up!

Thanks,

James
0
 
dsaboCommented:
You must initialize the variable in order to see the control at design time.


 You could do it at the constructor on the load event.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jamesnoeAuthor Commented:
But I have!

I have one file (let's call it myConstants) which looks like:

using System;

namespace My_Namespace
{
      public class myConstants
      {
            public static System.Windows.Forms.Label label1;

            public myConstants()
            {

            }
      }
}


In my MainApp file I have:

namespace My_Namespace
{
      public class MainApp : System.Windows.Forms.Form
      {
                      ......... stuff here ..........

            private void InitializeComponent()
            {
                  myConstants.label1= new System.Windows.Forms.Label();

                  this.summaryBox.Controls.Add(myConstants.label1);
            }
      }


There's obviously loads of stuff cut out - but am I doing something daft?

I'm quite new to C# - so apologies if this is daft.

Thanks for the quick responses.

Cheers,

James
0
 
Chester_M_RagelCommented:
I did the same thing and it worked fine for me... I'm not sure what is the problem in your code.. Can you try like this..


using System;

namespace My_Namespace
{
     public class myConstants
     {
          public static System.Windows.Forms.Label label1;

          public myConstants()
          {

          }
     }
}


namespace My_Namespace
{
     public class MainApp : System.Windows.Forms.Form
     {
                      ......... stuff here ..........

          private void InitializeComponent()
          {
               myConstants.label1= new System.Windows.Forms.Label();
               myConstants.label1.Location = new System.Drawing.Point(64, 40);
      myConstants.label1.Name = "label1";
      myConstants.label1.TabIndex = 1;
      myConstants.label1.Text = "label1";
      this.Controls.Add(myConstants.label1);
           }
     }
}
0
 
jamesnoeAuthor Commented:
Hi there,

Really odd!  I used your code, and I essentially get the same behaviour.  When I run the app I can see label1 - but when I go to the design view, I can't see it.

I copy/pasted your code in so I don't know if it's the code - any weird settings in Visual Studio...?

Any other coding options?

Thanks,

James
0
 
MaximKammererCommented:
An idea for a different solution:

Use a normal, windows-designer generated label; set it to invisible on runtime.
Copy all propertys from the normal label to your static label on runtime (if you plan to use it more than once, you could create a new control inheriting from label and add a CopyFrom method).

Best regards,
Maxim
0
 
jamesnoeAuthor Commented:
Hi.

Thanks for all the comments - however I simply can't get it working.

What I've done now is rather than using static variables, I'm simply passing by reference.

I think this is a bug in Visual Studio

Thanks again,

James
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now