• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 443
  • Last Modified:

Static C# class members

Hi,

If i create a C# class with a static member as follows:

static private PropertyBag settings;
private PropertyBag settingsLocal;

Does this exist only once for all instances of the class and the settingsLocal exist for each class instatiation?

How do i reference the static member?

the compiler complains as follows when I try to access "this.settings"

Error     8     Static member .......settings' cannot be accessed with an instance reference; qualify it with a type name instead     C:\......PropertiesFileReader.cs     160     8    

0
Christopher Schene
Asked:
Christopher Schene
  • 2
3 Solutions
 
DarrenDCommented:
As far as I know you must call the name of the class

classname.settings



Darren
0
 
DarrenDCommented:
When you use the 'this' keyword you are trying to access an instance of the sessings variable.

This is a static variable and so cannot be accessed via an instance of the class.

To access it outside the class call classname.settings

Also you may have to make it public.
0
 
Arthur_WoodCommented:
the error message tells you exactly what you did wrong, and how to avoid the error:

"Static member .......settings' cannot be accessed with an instance reference; qualify it with a type name instead"

You cannot use an Instance of the class (an Object created from the class), you use the type name (class name) instead:

in your case, it would be

<className>.settings   whatever your className is

AW
0
 
mjmarlowCommented:
Given your class has the following declarations:
static private PropertyBag settings;
private PropertyBag settingsLocal;

"settings" is static, but private.  It will not be accessable outside of the class you declare it in.  Define it as
static public PropertyBag settings; to make it available to other classes.   And yes, there is only one copy of "settings" property.

 "settingsLocal"  is and instance  property , and  every instance of your class will have a different "settingsLocal".

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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