473,657 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning byte[] in properties question..

Hi,

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

private byte[] bytes;

public byte[] ReturnsByteArra y
{
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.
Nov 17 '05 #1
5 9046
Ron
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" wrote:
Hi,

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

private byte[] bytes;

public byte[] ReturnsByteArra y
{
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.

Nov 17 '05 #2
I was wondering why FxCop 1.32 is complaining when I did so.
Can I ignore those "breaking" warnings.

"Ron" wrote:
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" wrote:
Hi,

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

private byte[] bytes;

public byte[] ReturnsByteArra y
{
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.

Nov 17 '05 #3
Ron
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" wrote:
I was wondering why FxCop 1.32 is complaining when I did so.
Can I ignore those "breaking" warnings.

"Ron" wrote:
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" wrote:
Hi,

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

private byte[] bytes;

public byte[] ReturnsByteArra y
{
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.

Nov 17 '05 #4
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" wrote:
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" wrote:
I was wondering why FxCop 1.32 is complaining when I did so.
Can I ignore those "breaking" warnings.

"Ron" wrote:
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" wrote:

> Hi,
>
> I've got a property that returns byte[].
> For example,
>
> private byte[] bytes;
>
> public byte[] ReturnsByteArra y
> {
> 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.

Nov 17 '05 #5
Ron
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" wrote:
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" wrote:
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" wrote:
I was wondering why FxCop 1.32 is complaining when I did so.
Can I ignore those "breaking" warnings.

"Ron" wrote:

> 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" wrote:
>
> > Hi,
> >
> > I've got a property that returns byte[].
> > For example,
> >
> > private byte[] bytes;
> >
> > public byte[] ReturnsByteArra y
> > {
> > 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.

Nov 17 '05 #6

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

Similar topics

15
2230
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
1
5209
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote peer. I have no problem connecting the peers and streaming Network Streams. When the incoming data is finished recieving, I act upon it. This works great as long as all of the code is inside my form. I want to build the networking code into a...
2
2093
by: Cory Burkhardt | last post by:
I am not sure what the suggested practice is for returning reference types from read-only properties. Consider an example: Suppose I have a Point class that is a reference type. I use the Point class from within a Shape class. I want the user of the Shape class to be able to see the location of the Shape but not be able to change it. So I declare a read-only property: class Shape { private Point location; /* other Shape stuff */
3
2689
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
3
12984
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
8
1838
by: MyAlias | last post by:
Can't solve this CallBack returning structures Error message: An unhandled exception of type 'System.NullReferenceException' occurred in MyTest.exe Additional information: Object reference not set to an instance of an object. Situation skeleton: Private Declare Function EnumFontFamiliesASCII Lib "gdi32" Alias
5
6329
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of uniqueidentifier, originally taken from objectGUID from active directory domain)
6
12484
by: rlcavebmg | last post by:
I am new to Web Services and .NET development, and I have a question. I am writing a service that will create a bitmap image and return it to the client. First, I wrote a method that looked like this: public System.Drawing.Bitmap CreateImage() {...} When I tried to create the web reference in my client program, I got an error message stating that the System.Drawing.Bitmap object could not be serialized because it does not have a...
4
1552
by: rbjorkquist | last post by:
This is my first attempt at writing/using web services, so any and all comments will be greatly appreciated. With that said, I am also by no means saying this is the correct either. I have noticed that when returning a new TPastePart, created and filled by the web service, that if the "PastePartID" and "PastePart" properties do not have "set" accessor methods, their respective data is not returned, even though the object has contains...
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.