1

I have created a library class where...

public class CircuitLibrary
{
   // Fields, properties, methods, etc.
   ...

   // Nested classes.
   public class Sensor
   {
      // Enums.
      public enum Sensors { Sensor1, Sensor2, Sensor3, ... };

      ...
   }

   public class SerialCommands
   {
      // Fields, properties, etc.
      ...

      // Nested classes.
      public class SensorSettingsCommands
      {
         // Fields, properties, etc.
         ...

         public void SomeMethod()
         {
            ...
            if( Sensor.Sensors.IsOn ) // Doesn't like this. OK if I change to CircuitLibrary.Sensor.Sensors.IsOn. Why?
               ...
          }
      }
   }
}

Here is the error I receive:

Cannot access a nonstatic member of outer type
"MyCircuitLibrary.CircuitLibrary.SerialCommands" via nested type
"MyCircuitLibrary.CircuitLibrary.SerialCommands.SensorSettingsCommands" 

So it looks like it is searching for (and found?) Sensor in SerialCommands? But if I change it to CircuitLibrary.Sensor it now knows it is in CircuitLibrary? When I right-click and "Go to definition" it finds it okay and doesn't say "Couldn't find Sensor in SerialCommands". If someone could help explain what is going on I would appreciate it.

Thanks!

3
  • I suspect some of the properties that you've not shown are relevant here. Please try to post a short but complete example. Commented Oct 28, 2011 at 19:38
  • 1
    BTW, you should not use public nested classes. Commented Oct 28, 2011 at 19:40
  • @SLaks: Yes, I read something similar here but considered my situation very similar to the described wheel-bearing example. I have a circuit controller which will use sensors (which can only be used on this type of circuit) and each sensor has properties I need to read/write. I have been battling this issue for a while now. :/ I'll open a new post for it; maybe I'll see you there. Thanks for your help. Commented Oct 28, 2011 at 20:07

1 Answer 1

3

Your SerialCommands class has a non-static Sensor property.

Since this property is closer to your code than the outer-most Sensor class, the compiler thinks you're using the property rather than the class.
Since you can't a use the non-static property without a SerialCommands instance, you get an error.

When you write CircuitLibrary.Sensor, it works fine, since there is no CircuitLibrary property to confuse the compiler.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.