Collection properties should be readonly.
-
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.
-
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.
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.”
-
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.
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.
-
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.
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
-
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.”
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.
-
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.
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.
-
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.
-
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.
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.