473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

FxCop, Properties and arrays


If I create an array, e.g. private int[] x, inside a parent class whose elements can be accessed by a child class - what is the
recommended way of doing this?

When I create a property for the array FxCop complains that properties should not return arrays. If I want to make the array
private, which FxCop insists I do as well, is the only way of accessing the array elements via a method?

See e.g. http://www.gotdotnet.com/team/fxcop/...urnArrays.html

the FxCop explanation describes what not to do but there is no explanation of what should be done instead.

Having Googled this I see that quite a number of people give the wrong way to do it and nobody seems to deal with the problem -
which seems to me to be a pretty basic one.

Thanks,
Steve

Nov 18 '05 #1
7 1935
Steve,
is the only way of accessing the array elements via a method?


Yes, is there any reason you don't want to use one in this case?
Mattias

Nov 18 '05 #2
you can also use the indexers for accessing elements of that array in the
child class. But for using indexer you should also define a property in your
base class which returns the current length of the array, so the in child
class you can iterate on the elements of that array.

i hope it solves your problem

"steve bull" wrote:

If I create an array, e.g. private int[] x, inside a parent class whose elements can be accessed by a child class - what is the
recommended way of doing this?

When I create a property for the array FxCop complains that properties should not return arrays. If I want to make the array
private, which FxCop insists I do as well, is the only way of accessing the array elements via a method?

See e.g. http://www.gotdotnet.com/team/fxcop/...urnArrays.html

the FxCop explanation describes what not to do but there is no explanation of what should be done instead.

Having Googled this I see that quite a number of people give the wrong way to do it and nobody seems to deal with the problem -
which seems to me to be a pretty basic one.

Thanks,
Steve

Nov 18 '05 #3


I would like to use FxCop because it is very good at uncovering some bugs and as MS appear to be behind it would seem to
me that if they don't want you to expose arrays to even child classes they should have a simple way of accessing them
via properties. Wouldn't methods add more overhead - especially when they are called many times?

Really I am just looking for the preferred method of doing this. Still learning at the moment.
Steve


On Fri, 18 Nov 2005 08:24:27 -0800, "Mattias Sjögren" <Ma***********@discussions.microsoft.com> wrote:
Steve,
is the only way of accessing the array elements via a method?


Yes, is there any reason you don't want to use one in this case?
Mattias

Nov 18 '05 #4
"steve bull" <bu****@comcast.net> wrote in message
news:hi********************************@4ax.com...


I would like to use FxCop because it is very good at uncovering some bugs and as MS appear to be
behind it would seem to
me that if they don't want you to expose arrays to even child classes they should have a simple
way of accessing them
via properties. Wouldn't methods add more overhead - especially when they are called many times?

Really I am just looking for the preferred method of doing this. Still learning at the moment.


Hi Steve,

Here is the argument that FxCop is using:

If you return an Array(even readonly) the contents can still be modified.
Thus, if you don't want the external world to modify YOUR internal array you should pass the world a
COPY of your array instead of Your actual Array. However, copies can be time consuming(or not,
depending on the size).
Now
People don't EXPECT properties to have side effects. They may put them in loops and call them
repeatedly. So it is a bad idea to, Say, access a Database in a property, or Read in a File, Or Copy
an Array.

All they are SUGGESTING is that instead of
public int[] Blah
{
get{return CopyMyArray();}
}

You should do this instead.
public int[] GetBlah()
{
return CopyMyArray();
}

The Amount of executed code is the same, but your intentions are more clear. An explicit GetBlah()
method says "I might take a while to do this".

Now, If you are the only person ever using the internal Array and you don't intend to ever modify
the Array outside of your class....You COULD break encapsulation and simply return a reference to
your array (I know...BLASPHEMY).
But If you choose to go this route, you should be aware of the reason for the rule in the first
place.

Good Luck
Bill


Nov 18 '05 #5

unfortunately the indexer cannot have a name so the index is on the class as a whole. which pretty much seems like a
waste of time. MS don't appear to have spent too much time thinking about this.
thanks,
steve

On Fri, 18 Nov 2005 08:30:44 -0800, "Murtuza" <Mu*****@discussions.microsoft.com> wrote:
you can also use the indexers for accessing elements of that array in the
child class. But for using indexer you should also define a property in your
base class which returns the current length of the array, so the in child
class you can iterate on the elements of that array.

i hope it solves your problem

"steve bull" wrote:

If I create an array, e.g. private int[] x, inside a parent class whose elements can be accessed by a child class - what is the
recommended way of doing this?

When I create a property for the array FxCop complains that properties should not return arrays. If I want to make the array
private, which FxCop insists I do as well, is the only way of accessing the array elements via a method?

See e.g. http://www.gotdotnet.com/team/fxcop/...urnArrays.html

the FxCop explanation describes what not to do but there is no explanation of what should be done instead.

Having Googled this I see that quite a number of people give the wrong way to do it and nobody seems to deal with the problem -
which seems to me to be a pretty basic one.

Thanks,
Steve

Nov 18 '05 #6

"steve bull" <bu****@comcast.net> wrote in message
news:a7********************************@4ax.com...

unfortunately the indexer cannot have a name so the index is on the class as a whole. which pretty
much seems like a
waste of time. MS don't appear to have spent too much time thinking about this.


This is true, however, If you really want a "Readonly Array", you can roll your own....sort of.
If you don't need all of the bells an whistles of the Array class, you can make a pseudoArray quite
easily.

This probably should be generic, but you get the idea
This is a bare minimum

// Minimal version
public class ReadOnlyIntArray
{
private int[] data;
public ReadOnlyIntArray(int[] data)
{
this.data = data;
}

public int this[int idx] {get{return(data[idx] );}} // didn't even bounds check
public int Length {get{return(data.Length);}}
}

This works fine for Accessing elements via indexer, but you can't do foreach since in does not
support IEnumerable.

If you want foreach support you can do this

// IEnumerable, IEnumerator version
public class ReadOnlyIntArray : IEnumerable, IEnumerator
{
private int currentIdx = -1;
private int[] data;
public ReadOnlyIntArray(int[] data)
{
this.data = data;
}

public int this[int idx] {get{return(data[idx] );}} // didn't even bounds check
public int Length {get{return(data.Length);}}

public IEnumerator GetEnumerator()
{
Reset();
return (this);
}

public object Current {get {return data[currentIdx]; }} // needs bounds check

public bool MoveNext()
{
bool ret = true;
currentIdx++;
if (currentIdx == data.Length)
ret = false;
return (ret);
}

public void Reset()
{
currentIdx = -1;
}
}

These classes were quick and dirty and need a bit of work to be more robust and behave correctly,
but you get the idea.

Hope this helps
Bill
Nov 18 '05 #7


this would limit me to one read only array per class. I have a polygon class and it will contain arrays of vertices and
arrays of edges etc. Generally I only want to iterate through the arrays. I will probably keep it simple and use a
method for access.

still seems to me that this is really poor design on MSs part.
thanks for the help,

steve

On Fri, 18 Nov 2005 17:49:50 GMT, "Bill Butler" <qw****@asdf.com> wrote:

"steve bull" <bu****@comcast.net> wrote in message
news:a7********************************@4ax.com.. .

unfortunately the indexer cannot have a name so the index is on the class as a whole. which pretty
much seems like a
waste of time. MS don't appear to have spent too much time thinking about this.


This is true, however, If you really want a "Readonly Array", you can roll your own....sort of.
If you don't need all of the bells an whistles of the Array class, you can make a pseudoArray quite
easily.

This probably should be generic, but you get the idea
This is a bare minimum

// Minimal version
public class ReadOnlyIntArray
{
private int[] data;
public ReadOnlyIntArray(int[] data)
{
this.data = data;
}

public int this[int idx] {get{return(data[idx] );}} // didn't even bounds check
public int Length {get{return(data.Length);}}
}

This works fine for Accessing elements via indexer, but you can't do foreach since in does not
support IEnumerable.

If you want foreach support you can do this

// IEnumerable, IEnumerator version
public class ReadOnlyIntArray : IEnumerable, IEnumerator
{
private int currentIdx = -1;
private int[] data;
public ReadOnlyIntArray(int[] data)
{
this.data = data;
}

public int this[int idx] {get{return(data[idx] );}} // didn't even bounds check
public int Length {get{return(data.Length);}}

public IEnumerator GetEnumerator()
{
Reset();
return (this);
}

public object Current {get {return data[currentIdx]; }} // needs bounds check

public bool MoveNext()
{
bool ret = true;
currentIdx++;
if (currentIdx == data.Length)
ret = false;
return (ret);
}

public void Reset()
{
currentIdx = -1;
}
}

These classes were quick and dirty and need a bit of work to be more robust and behave correctly,
but you get the idea.

Hope this helps
Bill

Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chua Wen Ching | last post by:
I had use fxcop to check my code. I had 1 confusion here. I would normally call a method by this way in my IAnimal: Example: public void CallFuncA(ushort port); But fxcop says i need to...
3
by: Rasmus | last post by:
I VS 2005 beta 2 i have a solution with - a number of classes - a website - a httphandler - a http module I want to run fxcop on my class files - but cant find out how to enable it. I've...
6
by: John Wright | last post by:
I ran FxCop against a program and was pleased with the security review except I get the following error: Do not indirectly expose methods How would I fix this code so this error goes away. I...
1
by: Water Cooler v2 | last post by:
I am trying to add FxCop to work inside Visual Studio 2005 Professional Edition. I believe there are two approaches. The first is outlined in this short article: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.