473,574 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2087

df********@hotm ail.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********@hotm ail.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********@hot mail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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 ReadOnlyCollect ion<string> Filenames
{
get { return filenames.AsRea dOnly(); }
}

// 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 ReadOnlyCollect ion<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 ScriptCollectio n
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 MyCollectionTyp e myCollection;
public MyCollectionTyp e MyCollection
{
get
{
if (myCollection == null)
myCollection = new MyCollectionTyp e();
return myCollection;
}
}

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"df********@hot mail.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********@hot mail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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***@DIESPAMM ERSDIEtakempis. 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Kevin Spencer <ke***@DIESPAMM ERSDIEtakempis. 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.co m>
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***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Kevin Spencer <ke***@DIESPAMM ERSDIEtakempis. 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.co m>
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

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

Similar topics

1
13287
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 alternative for outerHTML property in firefox. I am using th following function to display an image and alternate text behind al images of a weg...
19
4159
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 retrieve the values... (ie: application.currentproject.properties(propName)) b.) creating global variables in the form of an array and using a...
3
2682
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...
0
4431
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 is of typ OctetString, and i belive i read somewhere that i can use the convert.ToBase64string function to convert a byte array to a octetstring....
3
6118
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: "Object reference not set to object" This is what i'm doing below: // retrieve the array from the treenode tag property memberInfo = (string)tn.Tag;
3
1503
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 property and make its type equal to ClassB. but i would also like to index this property too. BTW Class B
30
2924
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
2498
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 any value to that color type property.. my code is Dim Col1Color as String
12
4974
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 store an array like this: protected static $_multiLevelArray = array('one'=> array(...), 'two'=> array(...)) protected static $_finalArray;
0
7826
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...
0
7741
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...
0
8259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8126
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...
0
6491
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...
1
5637
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...
0
5328
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...
0
3763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.