Thread: Variable protectiono level in Class

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16

    Variable protectiono level in Class

    With what protection level c# treats variable "a" in "myclas", because I get error code when I want to change it?

    Code:
    using system;
    
    class myclass {
       int a;
    }
    
    class mainclass {
    
       static void main() {
          myclass refmyCl = new myclass();
          
          refmyCl.a = 5; //error, protection level
       }
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Read about classes and default protection level here.

    It's good pratice to always set a protection level, so it's easier to see.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16
    Quote Originally Posted by nvoigt View Post
    Read about classes and default protection level here.

    It's good pratice to always set a protection level, so it's easier to see.
    Thx for good link, to answer my own question

    Types declared inside a class without an Access Modifier default to private.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You should listen to nvoigt. You should always specify the protection level and never rely on the default.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    When programming in C#, there doesn't tend to exist any pitfalls to the C# default accessibility levels (for both types and type members). This is so because they are all declared at the most restrictive possible level. So any errors coming of it will err on the side of privacy. Should easily be caught up by the compiler and will never put at risk your code. This might lead you to think that the advise give by nvoigt and VirtualAce is an unnecessary overstatement.

    It is not. Like with everything else in programming, trusting non-explicit conditions is soon or later going to bite you back in unexpected and more terrible ways than you can imagine. I'm not sure if the following will be understood by you just yet, but remains here for others to reference and you should at least get a general idea of what is happening.

    The C# Annotated Standard has a great example of a nasty potential pitfall. The following is a Test Class making use of the NUnit Framework (transcribed here):

    Code:
    using NUnit.Framework;
    
    namespace Company.WidgetLibTests
    {
        [TestFixture]
        class WidgetTests
        {
            [Test]
            public void SomeTest()
            {
                //...
            }
        }
    }
    If you compile this code, no error will be displayed. It's correct C# syntactically and semantically. If you load the test assembly into NUnit you should see your tests passing. All is well, you say. The test ran, the test passed, which means the code this method is testing is sound.

    But unknowing to you, this test never ran! You may spot or not that SomeTest() wasn't actually included in the list of tests on the tests results page. But I bet you, as you keep increasing the list of tests for this assembly (which can grow on to a hundred or more tests), you will not. Particularly if you were not the one coding the test.

    What happens is that the test class was implicitly declared as internal, since you didn't supply an explicit accessibility modifier. As such NUnit silently ignored your test. So you will get the wrong result set from your unit tests, which frankly is the worst thing that can happen to a coder: When you trust a unit test, but it lies to you. To spot the error in these conditions is very hard.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. an idea for a fourth access level for class members
    By Elkvis in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2012, 07:34 AM
  2. how to call a second-level base class method
    By Farnaz in forum C# Programming
    Replies: 1
    Last Post: 07-07-2011, 10:15 AM
  3. Replies: 2
    Last Post: 04-19-2011, 10:03 PM
  4. Need help with a class variable.
    By RealityFusion in forum C++ Programming
    Replies: 11
    Last Post: 10-20-2005, 10:26 AM
  5. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM

Tags for this Thread