Is there an example of how to store and access an array in a Property
Dave 10 2119 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
I am storing file names. The may be 1 or 1000. I was just reading an
article about indexers. Whould that work better? 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
<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.
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?
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?
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 page in Internet Explorer. Anybody can give solutio
for same in firefox browser.
function...
|
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
function to populate and retrieve them.
|
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)...
|
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.
This seem to be working since i can insert a image with the code:
|
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;
| |
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
|
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??
|
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
|
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;
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |