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

LVL 1
jamesnoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
Move this member to some generic (non-Control) class which is not handled by the Form Designer.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.