Click to See Complete Forum and Search --> : public static : inaccessable due to protection level!?
MushroomStamp
April 12th, 2004, 09:36 AM
If a member of one class is public static, what protection level does it have and how can you grant other classes acess to it? I would have expected the public-ness of it to suffice general reading priveleages to any class using the same namespace. What is wrong with my thinking?
TheCPUWizard
April 12th, 2004, 09:47 AM
Assuming the CLASS is public....nothing is wrong with your thinking....just with your doing...Please post a minimal / compilable snippet of code that exhibits the problem.
MushroomStamp
April 12th, 2004, 09:58 AM
namespace Logging
{
public class CQuickLog
{
public static string CLX_LOGFILE = "Clx_log.txt";
public static string PLX_LOGFILE = "Plx_log.txt";
CQuickLog(string sPlatform)
{
//...
}
}
}
and within other class
using Logging;
...
CQuickLog m_QuickLog;
...
m_QuickLog = new CQuickLog( CQuickLog.CLX_LOGFILE );
TheCPUWizard
April 12th, 2004, 10:04 AM
The access problem is on the constructor, not the static!
string s = CQuickLog.CLX_LOGFILE; // OK
CQuickLog ql = new CQuickLog( s); // Fails
MushroomStamp
April 12th, 2004, 10:09 AM
why? does it assume that the constructor might try to change the value of the string?
MushroomStamp
April 12th, 2004, 10:11 AM
WOW, sorry about that. i forgot to make the constructor public. i'm too used to c++.
MushroomStamp
April 12th, 2004, 10:13 AM
Just another thought...
Why don't c# ctors default to public? I've always thought of private ctors as just a shady means of enforcing abstractness...
TheCPUWizard
April 12th, 2004, 10:35 AM
Not at all....Consider the following to enforce parameter checks
public class C
{
private C(int x) {...}
public static C Build(int x)
{
if ((x%2) == 0) return new C(x) else return null;
}
}
..elsewhere..
C a = C.Build(3); // Will return NULL;
C b = C.Build(2); // Will return an instance;
disclaimer...not responsible for typos, this is only a sample....
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.