Solved

Static C# class members

Posted on 2006-11-30
4
434 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

6 Experts available now in Live!

Get 1:1 Help Now