Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. inaccessible due to protection level

inaccessible due to protection level

Scheduled Pinned Locked Moved C#
help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    I tried the following:

    class MonthRec
    {
    string month_text;
    string month_value;
    }

    private void run_code()
    {
    List MonthList = new List();
    MonthList.Add(new MonthRec(){month_text = "January 2009", month_value = "012009"});
    }

    but getting month_text inaccessible due to protection level same error for month_value

    P L 2 Replies Last reply
    0
    • J Jassim Rahma

      I tried the following:

      class MonthRec
      {
      string month_text;
      string month_value;
      }

      private void run_code()
      {
      List MonthList = new List();
      MonthList.Add(new MonthRec(){month_text = "January 2009", month_value = "012009"});
      }

      but getting month_text inaccessible due to protection level same error for month_value

      P Offline
      P Offline
      Peter Vertes
      wrote on last edited by
      #2

      By default if you don't specify an access modifier the scope of fields in a class default to private. Make both month_text and month_value public and you will be able to access them.

      --- http://iheartdotnet.blogspot.com

      1 Reply Last reply
      0
      • J Jassim Rahma

        I tried the following:

        class MonthRec
        {
        string month_text;
        string month_value;
        }

        private void run_code()
        {
        List MonthList = new List();
        MonthList.Add(new MonthRec(){month_text = "January 2009", month_value = "012009"});
        }

        but getting month_text inaccessible due to protection level same error for month_value

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        better yet, provide a constructor that accepts initial values and stores them. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        J 1 Reply Last reply
        0
        • L Luc Pattyn

          better yet, provide a constructor that accepts initial values and stores them. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #4

          i did declare as public but now I am getting three columns! 1. Column with value myappname.myformname+MonthRec 2. month_value with no value 3. month_text with no value

          L 1 Reply Last reply
          0
          • J Jassim Rahma

            i did declare as public but now I am getting three columns! 1. Column with value myappname.myformname+MonthRec 2. month_value with no value 3. month_text with no value

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            you should not make public what does not need to be public; my suggestion was and is keep the data members private (or protected) and provide a means to set them, either through properties or through a new constructor that takes initial values. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups