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
  1. Home
  2. General Programming
  3. C#
  4. Collection properties should be readonly.

Collection properties should be readonly.

Scheduled Pinned Locked Moved C#
helpvisual-studiocomquestion
8 Posts 5 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.
  • D Offline
    D Offline
    deep7
    wrote on last edited by
    #1

    Hi, According to FXCop, i must have arraylist property as read only (not setter). i tried below method as was mentioned in msdn site(http://msdn.microsoft.com/en-us/library/ms182327(VS.80).aspx): public ArrayList SomeStrings { get { return strings; } // Violates the rule. // set { strings = value; } } ArrayList newCollection = new ArrayList(); WritableCollection collection = new WritableCollection(); collection.SomeStrings = newCollection; collection.SomeStrings.Clear(); collection.SomeStrings.AddRange(newCollection); But at the point marked bold italic, i get error saying that the property is readonly. I'm really stuck here, can anyone plz give m solution? I need to set data to the arraylist, but its not FXCop compliant.

    H L L S 4 Replies Last reply
    0
    • D deep7

      Hi, According to FXCop, i must have arraylist property as read only (not setter). i tried below method as was mentioned in msdn site(http://msdn.microsoft.com/en-us/library/ms182327(VS.80).aspx): public ArrayList SomeStrings { get { return strings; } // Violates the rule. // set { strings = value; } } ArrayList newCollection = new ArrayList(); WritableCollection collection = new WritableCollection(); collection.SomeStrings = newCollection; collection.SomeStrings.Clear(); collection.SomeStrings.AddRange(newCollection); But at the point marked bold italic, i get error saying that the property is readonly. I'm really stuck here, can anyone plz give m solution? I need to set data to the arraylist, but its not FXCop compliant.

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      I do not see your problem. The error line functionality is achieved in the two lines below it. Same effect, just slightly longer to code, and possibly slightly slower to run. More to the point, do you have to use an ArrayList? You would be better off using List<string>. :)

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      L 1 Reply Last reply
      0
      • D deep7

        Hi, According to FXCop, i must have arraylist property as read only (not setter). i tried below method as was mentioned in msdn site(http://msdn.microsoft.com/en-us/library/ms182327(VS.80).aspx): public ArrayList SomeStrings { get { return strings; } // Violates the rule. // set { strings = value; } } ArrayList newCollection = new ArrayList(); WritableCollection collection = new WritableCollection(); collection.SomeStrings = newCollection; collection.SomeStrings.Clear(); collection.SomeStrings.AddRange(newCollection); But at the point marked bold italic, i get error saying that the property is readonly. I'm really stuck here, can anyone plz give m solution? I need to set data to the arraylist, but its not FXCop compliant.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Why are you using an ArrayList anyway? Are you using .NET 1? Some things you could do: - Create the new collection in the ctor of the containing class - Make a method that takes an ArrayList and assigns that to the field/property which can then have a private setter (or no setter, if it's a field). This is not actually a good way though, it just sidesteps the problem and may generate an other warning - Make a method that takes items to add to the ArrayList which may then not have to be exposed to the outside anymore - Stop caring about FXCop (hey it's possible), what it says are not rules, but just guidelines. If you have good reason to ignore its advice then do it - or at least do not blindly follow its advice, following a mere guideline can cause an actual error (as is the case now). Following the guidelines can lead to a good design, but also to hacking around just to follow guidelines while actually making the design worse.

        L 1 Reply Last reply
        0
        • D deep7

          Hi, According to FXCop, i must have arraylist property as read only (not setter). i tried below method as was mentioned in msdn site(http://msdn.microsoft.com/en-us/library/ms182327(VS.80).aspx): public ArrayList SomeStrings { get { return strings; } // Violates the rule. // set { strings = value; } } ArrayList newCollection = new ArrayList(); WritableCollection collection = new WritableCollection(); collection.SomeStrings = newCollection; collection.SomeStrings.Clear(); collection.SomeStrings.AddRange(newCollection); But at the point marked bold italic, i get error saying that the property is readonly. I'm really stuck here, can anyone plz give m solution? I need to set data to the arraylist, but its not FXCop compliant.

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

          Hi, FYI: cops aren't always right, and you can change the rules if you don't like them. The MSDN example looks OK. You must choose between 2 possibilities: 1. SomeStrings has no setter, and collection.SomeStrings = newCollection; does not compile, however you can use Clear() and AddRange() instead, as suggested by the MSDN page. 2. SomeStrings has a setter, collection.SomeStrings = newCollection; works, and unmodified FXCop complains, however FXCop with a new or modified rule could be made to agree :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          modified on Monday, August 24, 2009 9:23 AM

          1 Reply Last reply
          0
          • H Henry Minute

            I do not see your problem. The error line functionality is achieved in the two lines below it. Same effect, just slightly longer to code, and possibly slightly slower to run. More to the point, do you have to use an ArrayList? You would be better off using List<string>. :)

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

            Henry Minute wrote:

            do you have to use an ArrayList?

            Sure, however the referenced MSDN page[^], although part of VS8, gave an example with ArrayList (which probably dated back from .NET 1.x) :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            1 Reply Last reply
            0
            • L Lost User

              Why are you using an ArrayList anyway? Are you using .NET 1? Some things you could do: - Create the new collection in the ctor of the containing class - Make a method that takes an ArrayList and assigns that to the field/property which can then have a private setter (or no setter, if it's a field). This is not actually a good way though, it just sidesteps the problem and may generate an other warning - Make a method that takes items to add to the ArrayList which may then not have to be exposed to the outside anymore - Stop caring about FXCop (hey it's possible), what it says are not rules, but just guidelines. If you have good reason to ignore its advice then do it - or at least do not blindly follow its advice, following a mere guideline can cause an actual error (as is the case now). Following the guidelines can lead to a good design, but also to hacking around just to follow guidelines while actually making the design worse.

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

              harold aptroot wrote:

              Why are you using an ArrayList anyway?

              Right, however the referenced MSDN page[^], although part of VS8, gave an example with ArrayList (which probably dated back from .NET 1.x) :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              L 1 Reply Last reply
              0
              • L Luc Pattyn

                harold aptroot wrote:

                Why are you using an ArrayList anyway?

                Right, however the referenced MSDN page[^], although part of VS8, gave an example with ArrayList (which probably dated back from .NET 1.x) :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Alright, so that site has an excuse, but he could/should have changed it IMO

                1 Reply Last reply
                0
                • D deep7

                  Hi, According to FXCop, i must have arraylist property as read only (not setter). i tried below method as was mentioned in msdn site(http://msdn.microsoft.com/en-us/library/ms182327(VS.80).aspx): public ArrayList SomeStrings { get { return strings; } // Violates the rule. // set { strings = value; } } ArrayList newCollection = new ArrayList(); WritableCollection collection = new WritableCollection(); collection.SomeStrings = newCollection; collection.SomeStrings.Clear(); collection.SomeStrings.AddRange(newCollection); But at the point marked bold italic, i get error saying that the property is readonly. I'm really stuck here, can anyone plz give m solution? I need to set data to the arraylist, but its not FXCop compliant.

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #8

                  I suggest: public List<string> SomeStrings { get; private set; }

                  A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                  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