473,326 Members | 2,805 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,326 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 2064

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: prasaddevivara | last post by:
I am using the outerHTML property to modify the HTML of existin elements in a web page in Internet Explorer. But same outerHTM property is not working in firefox browser, Anybody can tell me a...
19
by: laurenq uantrell | last post by:
I'm using Access 2K. I'm hoping someone can tell me which method performs faster- (currently I'm using a mix of both methods) a.) creating custom properties and then calling functions to set and...
3
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...
0
by: MS Newsgroups | last post by:
Hi, I am trying to store a thumbnail picture in ActiveDirectory, but am having some problems with type conversions. According to the documentation the Thumbnailphoto property in the directory...
3
by: Ant | last post by:
Hi, I'm using the tag property of the Treenode object to store an string array containing some values. I can assign it to the Tag but when I try to retrieve values from it I get an error:...
3
by: Michael Matteson | last post by:
I have two classes. Class A and Class B. I give class A 5 properties int prop1(){} int prop2(){} int prop3(){} int prop4(){} classB prop5(){} what i would like to do is to create a 5th...
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
4
by: Avi | last post by:
Hi I am creating web application in which i want to assign by default values to the property which i had created my own. In that one of the property is of type color and i am unable to assign...
12
bilibytes
by: bilibytes | last post by:
hi, i need some help here. how can i access a static property of a subclass. i have an abstract class which will be extended... each extending class will have a static property that will...
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...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.