471,337 Members | 1,178 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Property to store an Array???

Is there an example of how to store and access an array in a Property

Dave

Feb 2 '06 #1
10 1982

df********@hotmail.com wrote:
Is there an example of how to store and access an array in a Property


Not sure what you mean. If you mean a property that IS an array, it
works fine:

private int[] intArray = new int[20];
public int[] IntArray
{
get { return intArray; }
set { intArray = value; }
}

// and then...

myobject m = new myobject();
int[] curArray = m.IntArray;

Is that what you were trying to do?

matt

Feb 2 '06 #2
I am storing file names. The may be 1 or 1000. I was just reading an
article about indexers. Whould that work better?

Feb 2 '06 #3

df********@hotmail.com wrote:
I am storing file names. The may be 1 or 1000. I was just reading an
article about indexers. Whould that work better?


As with most things, it really is up to you. You could use an ArrayList
(1.1)
or a generic array (2.0) to store your data and then return it using an
indexer.
You could simply override the operator[] and return whatever you want.
What
you really need to decide is what you are providing access to. C#
provides
lots of syntactic sugar methods for making things look easy to the end
user.
That doesn't mean any of them are any "better" than anything else.

Matt

Feb 2 '06 #4

<df********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I am storing file names. The may be 1 or 1000. I was just reading an
article about indexers. Whould that work better?


You should only use an indexer directly if your class IS a collection.

If it just holds a collection (Seems more likely) then you should probably
have a get-only property to hold the collection and probably make it read
only to the user.

class X
{
List<string> filenames;

public ReadOnlyCollection<string> Filenames
{
get { return filenames.AsReadOnly(); }
}

// And indexer - just for reference
public string this[int i]
{
get { return filenames[i]; }
}

// If you have an indexer then you must have the length somewhere
public int Count { get { return filenames.Count; }
}

The AsReadOnly() method is an excellent reason to use List<string> rather
than string[].

Most examples use IList<T> rather than ReadOnlyCollection<T> as the property
type but this means it seems to me that my way makes it obvious that the
collection is read only without having to read the documentation for the
property.

Feb 2 '06 #5
If you're using .Net 1.1, I suggest that you create a collection from
CollectionBase. You can see a simple example of that in the ScriptCollection
my ClientScripts control at
http://www.dalepreston.com/Blog/2005...s-to-your.html or in Natty Gur's more detailed example referenced in my article.

When you create your custom collection, you make a read only property so
that the entire collection is not replaced but rather you update/add/insert,
etc. only items within the collection for instance:

private MyCollectionType myCollection;
public MyCollectionType MyCollection
{
get
{
if (myCollection == null)
myCollection = new MyCollectionType();
return myCollection;
}
}

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"df********@hotmail.com" wrote:
I am storing file names. The may be 1 or 1000. I was just reading an
article about indexers. Whould that work better?

Feb 2 '06 #6
An array is by far more lightweight (read "less overhead") than a
Collection. Therefore, when you can get away with an array, it will be
better for your application overall in terms of resource usage and
performance. You only need a Collection if you need to add, or remove items
at any time. This is because an array is immutable (fixed size). When you
change the size of an array, you duplicate the entire array (that you are
keeping). A Collection is not of a fixed size. If you are simply inputting
(for example) 1000 strings one time, and reading it after that, use an
array.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

<df********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I am storing file names. The may be 1 or 1000. I was just reading an
article about indexers. Whould that work better?

Feb 2 '06 #7
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
An array is by far more lightweight (read "less overhead") than a
Collection. Therefore, when you can get away with an array, it will be
better for your application overall in terms of resource usage and
performance. You only need a Collection if you need to add, or remove items
at any time. This is because an array is immutable (fixed size). When you
change the size of an array, you duplicate the entire array (that you are
keeping). A Collection is not of a fixed size. If you are simply inputting
(for example) 1000 strings one time, and reading it after that, use an
array.


However, although an array has a fixed size, it *isn't* generally
immutable (i.e. anyone can change the contents). With collections, as
Nick pointed out, you can expose a read-only collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 2 '06 #8
True, it's not truly immutable. What I meant was that it has a fixed number
of elements.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
An array is by far more lightweight (read "less overhead") than a
Collection. Therefore, when you can get away with an array, it will be
better for your application overall in terms of resource usage and
performance. You only need a Collection if you need to add, or remove
items
at any time. This is because an array is immutable (fixed size). When you
change the size of an array, you duplicate the entire array (that you are
keeping). A Collection is not of a fixed size. If you are simply
inputting
(for example) 1000 strings one time, and reading it after that, use an
array.


However, although an array has a fixed size, it *isn't* generally
immutable (i.e. anyone can change the contents). With collections, as
Nick pointed out, you can expose a read-only collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 2 '06 #9
I don't know about you but I don't remember ever coming across a situation
where it was OK for a user of my class to change the elements of a
collection it held but not to change the size.

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
True, it's not truly immutable. What I meant was that it has a fixed
number of elements.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
An array is by far more lightweight (read "less overhead") than a
Collection. Therefore, when you can get away with an array, it will be
better for your application overall in terms of resource usage and
performance. You only need a Collection if you need to add, or remove
items
at any time. This is because an array is immutable (fixed size). When
you
change the size of an array, you duplicate the entire array (that you
are
keeping). A Collection is not of a fixed size. If you are simply
inputting
(for example) 1000 strings one time, and reading it after that, use an
array.


However, although an array has a fixed size, it *isn't* generally
immutable (i.e. anyone can change the contents). With collections, as
Nick pointed out, you can expose a read-only collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Feb 3 '06 #10
How is a collection more overhead? I mean, obviously there is some overhead,
but how is it so significant that you would reject it?
And besides the ability to add or remove items, a collection also provides
methods like Insert so you can put something in the place you want it; it
includes IndexOf, Contains, etc. that allow you to find a specific item, and
even better, collections include indexers other than int.

I can take my simple collection shell, do a replace all to change the type
it handles, and have a custom collection working in most cases in about 10
minutes. And every time I use any of the collection features later on, I am
glad I did. On the other hand, many times when I avoid doing creating a
custom collection, thinking an array will be enough, I find myself making
compromises or wishing I had created the collection.

If I start with a collection I never find myself wishing I could go back to
a simple array but if I start with an array, I often wish I had a collection
instead.

That's just my experience. your mileage or the OP's mileage may vary :)

--
Dale Preston
MCAD C#
MCSE, MCDBA
"Kevin Spencer" wrote:
True, it's not truly immutable. What I meant was that it has a fixed number
of elements.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
An array is by far more lightweight (read "less overhead") than a
Collection. Therefore, when you can get away with an array, it will be
better for your application overall in terms of resource usage and
performance. You only need a Collection if you need to add, or remove
items
at any time. This is because an array is immutable (fixed size). When you
change the size of an array, you duplicate the entire array (that you are
keeping). A Collection is not of a fixed size. If you are simply
inputting
(for example) 1000 strings one time, and reading it after that, use an
array.


However, although an array has a fixed size, it *isn't* generally
immutable (i.e. anyone can change the contents). With collections, as
Nick pointed out, you can expose a read-only collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Feb 3 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by laurenq uantrell | last post: by
reply views Thread by MS Newsgroups | last post: by
3 posts views Thread by Ant | last post: by
30 posts views Thread by josh | last post: by
bilibytes
12 posts views Thread by bilibytes | last post: by
reply views Thread by rosydwin | last post: by

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.