Solved

Static C# class members

Posted on 2006-11-30
4
437 Views
Last Modified: 2010-04-16
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
Comment
Question by:Christopher Schene
  • 2
4 Comments
 
LVL 18

Expert Comment

by:DarrenD
ID: 18046587
As far as I know you must call the name of the class

classname.settings



Darren
0
 
LVL 18

Assisted Solution

by:DarrenD
DarrenD earned 150 total points
ID: 18046659
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
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 250 total points
ID: 18047304
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
 
LVL 7

Assisted Solution

by:mjmarlow
mjmarlow earned 100 total points
ID: 18051059
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

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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