|
-
April 12th, 2004, 09:36 AM
#1
public static : inaccessable due to protection level!?
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?
-
April 12th, 2004, 09:47 AM
#2
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
April 12th, 2004, 09:58 AM
#3
Code:
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
Code:
using Logging;
...
CQuickLog m_QuickLog;
...
m_QuickLog = new CQuickLog( CQuickLog.CLX_LOGFILE );
-
April 12th, 2004, 10:04 AM
#4
The access problem is on the constructor, not the static!
Code:
string s = CQuickLog.CLX_LOGFILE; // OK
CQuickLog ql = new CQuickLog( s); // Fails
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
April 12th, 2004, 10:09 AM
#5
why? does it assume that the constructor might try to change the value of the string?
-
April 12th, 2004, 10:11 AM
#6
WOW, sorry about that. i forgot to make the constructor public. i'm too used to c++.
-
April 12th, 2004, 10:13 AM
#7
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...
-
April 12th, 2004, 10:35 AM
#8
Not at all....Consider the following to enforce parameter checks
Code:
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....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|