CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37

    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?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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

  3. #3
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37
    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 );

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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

  5. #5
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37
    why? does it assume that the constructor might try to change the value of the string?

  6. #6
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37
    WOW, sorry about that. i forgot to make the constructor public. i'm too used to c++.

  7. #7
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37
    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...

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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
  •  





Click Here to Expand Forum to Full Width

Featured