Protection Level

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    #1

    Protection Level

    I am getting a "Protection Level Error" when I access a private variable
    (numberOfEngine s) from my subclass SingleEnginePla ne. If I set it as public
    I can access it directly from my object in my Main function.

    I would like the subclass to access it directly but not directly from my
    function.

    I would like to access it by:

    myPlane.NumberO fEngines (property)

    and not

    myPlane._number OfEngines (variable)

    But at the same time be able to access _numberOfEngine s directly from the
    subclass.

    *************** *************** *****
    using System;

    namespace ConsoleApplicat ion13
    {
    public abstract class Plane
    {
    public string tailNumber;
    private int _numberOfEngine s;
    public string TailNumber
    {
    get
    {
    return tailNumber;
    }
    set
    {
    tailNumber = value;
    }
    }
    public int NumberOfEngines
    {
    get
    {
    return _numberOfEngine s;
    }
    set
    {
    _numberOfEngine s = value;
    }
    }
    }

    public class SingleEnginePla ne : Plane
    {
    public SingleEnginePla ne()
    {
    _numberOfEngine s = 1; <--- the error
    }
    }

    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    SingleEnginePla ne myPlane = new SingleEnginePla ne();

    myPlane.TailNum ber = "150";

    Console.WriteLi ne(String.Forma t("My 1st Planes Tail Number is: {0}",
    myPlane.TailNum ber));
    Console.WriteLi ne(String.Forma t("My 1st Plane has {0} engines:
    ",myPlane._numb erOfEngines));
    Console.WriteLi ne(String.Forma t("My 1st Plane has {0} engines:
    ",myPlane.Numbe rOfEngines));
    Console.Read();
    }
    }
    }
    *************** *************** *************** *************** *******

    Thanks,

    Tom


  • Peter Duniho

    #2
    Re: Protection Level

    tshad wrote:
    [...]
    But at the same time be able to access _numberOfEngine s directly from the
    subclass.
    Then the member variable needs to be "protected" rather than "private".

    Pete

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Protection Level

      tshad,

      I think you mean you want to access it using myPlace._number OfEngines,
      as you can already access it from the property.

      If you want to access the variable, you have to change the access
      modifier of the variable from private to internal or protected.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "tshad" <tfs@dslextreme .comwrote in message
      news:OLgyc0QEIH A.4140@TK2MSFTN GP03.phx.gbl...
      >I am getting a "Protection Level Error" when I access a private variable
      >(numberOfEngin es) from my subclass SingleEnginePla ne. If I set it as
      >public I can access it directly from my object in my Main function.
      >
      I would like the subclass to access it directly but not directly from my
      function.
      >
      I would like to access it by:
      >
      myPlane.NumberO fEngines (property)
      >
      and not
      >
      myPlane._number OfEngines (variable)
      >
      But at the same time be able to access _numberOfEngine s directly from the
      subclass.
      >
      *************** *************** *****
      using System;
      >
      namespace ConsoleApplicat ion13
      {
      public abstract class Plane
      {
      public string tailNumber;
      private int _numberOfEngine s;
      public string TailNumber
      {
      get
      {
      return tailNumber;
      }
      set
      {
      tailNumber = value;
      }
      }
      public int NumberOfEngines
      {
      get
      {
      return _numberOfEngine s;
      }
      set
      {
      _numberOfEngine s = value;
      }
      }
      }
      >
      public class SingleEnginePla ne : Plane
      {
      public SingleEnginePla ne()
      {
      _numberOfEngine s = 1; <--- the error
      }
      }
      >
      class Class1
      {
      [STAThread]
      static void Main(string[] args)
      {
      SingleEnginePla ne myPlane = new SingleEnginePla ne();
      >
      myPlane.TailNum ber = "150";
      >
      Console.WriteLi ne(String.Forma t("My 1st Planes Tail Number is: {0}",
      myPlane.TailNum ber));
      Console.WriteLi ne(String.Forma t("My 1st Plane has {0} engines:
      ",myPlane._numb erOfEngines));
      Console.WriteLi ne(String.Forma t("My 1st Plane has {0} engines:
      ",myPlane.Numbe rOfEngines));
      Console.Read();
      }
      }
      }
      *************** *************** *************** *************** *******
      >
      Thanks,
      >
      Tom
      >

      Comment

      • tshad

        #4
        Re: Protection Level

        "Peter Duniho" <NpOeStPeAdM@Nn OwSlPiAnMk.comw rote in message
        news:13hd46h5ni pn4ce@corp.supe rnews.com...
        tshad wrote:
        >[...]
        >But at the same time be able to access _numberOfEngine s directly from the
        >subclass.
        >
        Then the member variable needs to be "protected" rather than "private".
        That was it.

        Thanks,

        Tom
        >
        Pete

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Protection Level

          tshad <tfs@dslextreme .comwrote:
          "Peter Duniho" <NpOeStPeAdM@Nn OwSlPiAnMk.comw rote in message
          news:13hd46h5ni pn4ce@corp.supe rnews.com...
          tshad wrote:
          [...]
          But at the same time be able to access _numberOfEngine s directly from the
          subclass.
          Then the member variable needs to be "protected" rather than "private".
          >
          That was it.
          However, that's usually a bad idea, IMO. I prefer to make all fields
          private, and use properties even for derived class access.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • active T

            #6
            Re: Protection Level

            You can always access the property because that's a public property.
            In that case it doesn't mather.

            it's also posible to combine the properties and access modifiers:

            just like:

            public int NumberOfEngines
            {
            get
            {
            return _numberOfEngine s;
            }
            protected set
            {
            _numberOfEngine s = value;
            }
            }

            in that case the protection level of the Set is lower than the Get.

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Protection Level

              On Oct 18, 11:10 am, active T <TonnyBr...@gma il.comwrote:
              You can always access the property because that's a public property.
              In that case it doesn't mather.
              >
              it's also posible to combine the properties and access modifiers:
              <snip>

              Note that this is C# 2 specific. In C# 1 the get/set had to have the
              same access.

              Jon

              Comment

              • tshad

                #8
                Re: Protection Level

                "active T" <TonnyBrink@gma il.comwrote in message
                news:1192702259 .328771.118350@ z24g2000prh.goo glegroups.com.. .
                You can always access the property because that's a public property.
                In that case it doesn't mather.
                >
                it's also posible to combine the properties and access modifiers:
                >
                just like:
                >
                public int NumberOfEngines
                {
                get
                {
                return _numberOfEngine s;
                }
                protected set
                {
                _numberOfEngine s = value;
                }
                }
                >
                in that case the protection level of the Set is lower than the Get.
                That's interesting.

                I hadn't seen that before.

                Thanks,

                Tom


                Comment

                Working...