Solved

Static C# class members

Posted on 2006-11-30
4
438 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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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