returning byte[] in properties question..

G

Guest

Hi,

I've got a property that returns byte[].
For example,

private byte[] bytes;

public byte[] ReturnsByteArray
{
get
{
return bytes;
}
set
{
bytes = value;
}
}

It compiled well, however, FxCop 1.32 is suggesting that..
"Properties should not return Arrays".

When I replaced the property with two methods, for example

public byte[] GetBytes()
{
return bytes;
}
public void SetBytes(byte[] by)
{
bytes = by;
}

Then, FxCop is suggesting this..
"Use properties where appropriate" pointing at "SetBytes(..)" method.

When I made my private byte[] bytes as public, then it is suggesting
not to expose variables, it suggests use of properties instead.

Kindly let me know how I can resolve this problem.

Cheers,

Naveen.
 
G

Guest

Nothing stops you from returning an array from a Property. In fact, you will
find code snippets in the MSDN Library that do so.
 
G

Guest

I was wondering why FxCop 1.32 is complaining when I did so.
Can I ignore those "breaking" warnings.

Ron said:
Nothing stops you from returning an array from a Property. In fact, you will
find code snippets in the MSDN Library that do so.

Naveen Mukkelli said:
Hi,

I've got a property that returns byte[].
For example,

private byte[] bytes;

public byte[] ReturnsByteArray
{
get
{
return bytes;
}
set
{
bytes = value;
}
}

It compiled well, however, FxCop 1.32 is suggesting that..
"Properties should not return Arrays".

When I replaced the property with two methods, for example

public byte[] GetBytes()
{
return bytes;
}
public void SetBytes(byte[] by)
{
bytes = by;
}

Then, FxCop is suggesting this..
"Use properties where appropriate" pointing at "SetBytes(..)" method.

When I made my private byte[] bytes as public, then it is suggesting
not to expose variables, it suggests use of properties instead.

Kindly let me know how I can resolve this problem.

Cheers,

Naveen.
 
G

Guest

I guess it all comes down to efficiency. Take a look at this link:
http://msdn2.microsoft.com/en-us/library/k2604h5s. What FxCop is complaining
about is that you are not following a guideline. If you keep it as it is, it
won't break anything, but it will most likely create unwanted copies of the
array depending upon its use.

Naveen Mukkelli said:
I was wondering why FxCop 1.32 is complaining when I did so.
Can I ignore those "breaking" warnings.

Ron said:
Nothing stops you from returning an array from a Property. In fact, you will
find code snippets in the MSDN Library that do so.

Naveen Mukkelli said:
Hi,

I've got a property that returns byte[].
For example,

private byte[] bytes;

public byte[] ReturnsByteArray
{
get
{
return bytes;
}
set
{
bytes = value;
}
}

It compiled well, however, FxCop 1.32 is suggesting that..
"Properties should not return Arrays".

When I replaced the property with two methods, for example

public byte[] GetBytes()
{
return bytes;
}
public void SetBytes(byte[] by)
{
bytes = by;
}

Then, FxCop is suggesting this..
"Use properties where appropriate" pointing at "SetBytes(..)" method.

When I made my private byte[] bytes as public, then it is suggesting
not to expose variables, it suggests use of properties instead.

Kindly let me know how I can resolve this problem.

Cheers,

Naveen.
 
G

Guest

Hi,

Thanks for the link. Its a good reference.

I've tried this..

private byte[] bytes;
public byte[] Bytes
{
set
{
bytes = value;
}
}
public byte[] GetBytes()
{
return bytes;
}

FxCop still complains point at the property, even though I'm trying to set
the
byte[].

Can you please suggest me what is the best way to set a byte[], is it using
a property or a method.

Kindly let me know.

Cheers,

Naveen.

Ron said:
I guess it all comes down to efficiency. Take a look at this link:
http://msdn2.microsoft.com/en-us/library/k2604h5s. What FxCop is complaining
about is that you are not following a guideline. If you keep it as it is, it
won't break anything, but it will most likely create unwanted copies of the
array depending upon its use.

Naveen Mukkelli said:
I was wondering why FxCop 1.32 is complaining when I did so.
Can I ignore those "breaking" warnings.

Ron said:
Nothing stops you from returning an array from a Property. In fact, you will
find code snippets in the MSDN Library that do so.

:

Hi,

I've got a property that returns byte[].
For example,

private byte[] bytes;

public byte[] ReturnsByteArray
{
get
{
return bytes;
}
set
{
bytes = value;
}
}

It compiled well, however, FxCop 1.32 is suggesting that..
"Properties should not return Arrays".

When I replaced the property with two methods, for example

public byte[] GetBytes()
{
return bytes;
}
public void SetBytes(byte[] by)
{
bytes = by;
}

Then, FxCop is suggesting this..
"Use properties where appropriate" pointing at "SetBytes(..)" method.

When I made my private byte[] bytes as public, then it is suggesting
not to expose variables, it suggests use of properties instead.

Kindly let me know how I can resolve this problem.

Cheers,

Naveen.
 
G

Guest

Remember that an array is a reference type, so a user can still modify its
elements
without using your property, ie, when calling GetBytes(). If you must use a
property, you have to return a copy of the array so that you have control
over what gets written to it. Still, this is inefficient. Consider using a
method that returns an array or a public array member.

Naveen Mukkelli said:
Hi,

Thanks for the link. Its a good reference.

I've tried this..

private byte[] bytes;
public byte[] Bytes
{
set
{
bytes = value;
}
}
public byte[] GetBytes()
{
return bytes;
}

FxCop still complains point at the property, even though I'm trying to set
the
byte[].

Can you please suggest me what is the best way to set a byte[], is it using
a property or a method.

Kindly let me know.

Cheers,

Naveen.

Ron said:
I guess it all comes down to efficiency. Take a look at this link:
http://msdn2.microsoft.com/en-us/library/k2604h5s. What FxCop is complaining
about is that you are not following a guideline. If you keep it as it is, it
won't break anything, but it will most likely create unwanted copies of the
array depending upon its use.

Naveen Mukkelli said:
I was wondering why FxCop 1.32 is complaining when I did so.
Can I ignore those "breaking" warnings.

:

Nothing stops you from returning an array from a Property. In fact, you will
find code snippets in the MSDN Library that do so.

:

Hi,

I've got a property that returns byte[].
For example,

private byte[] bytes;

public byte[] ReturnsByteArray
{
get
{
return bytes;
}
set
{
bytes = value;
}
}

It compiled well, however, FxCop 1.32 is suggesting that..
"Properties should not return Arrays".

When I replaced the property with two methods, for example

public byte[] GetBytes()
{
return bytes;
}
public void SetBytes(byte[] by)
{
bytes = by;
}

Then, FxCop is suggesting this..
"Use properties where appropriate" pointing at "SetBytes(..)" method.

When I made my private byte[] bytes as public, then it is suggesting
not to expose variables, it suggests use of properties instead.

Kindly let me know how I can resolve this problem.

Cheers,

Naveen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top