Solved

Static C# class members

Posted on 2006-11-30
4
436 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:cschene
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WKHTMLTOPDF - --disable-smart-shrinking not working 10 80
Run time Error 4 47
Error on link 14 46
How to parse the string and split the sub directory names in asp.net 7 38
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now