Link to home
Start Free TrialLog in
Avatar of jamesnoe
jamesnoeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jamesnoe

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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