-
Looking for a VB Collections style example in C#
Hi,
If any C# gurus out there know how to implement a collection that is VB-styled, please show me how.
Methods wanted:
<objectcollection>.Add (object) -- add object to collection
<objectcollection>.Remove (index) -- remove object by index
<objectcollection>.Item(index) -- to return an object by index
<objectcolelction>.Count()
Additionally, it would be nice to iterate through it using 'foreach'.
The basic collection types seem OK to me with the exception that it appears quite difficult to get one to easily return an item by it's index value - this is the problem - doing it by key or object comparison seems well supported. In my particular scenario
, the collection is a group of objects of the same type. I would like to know alot more about collections and collection classes in C#. Any help is seriously appreciated.
Many thanks,
Conrad
--
Sent via http://csharpindex.com
C# Index to resources
-
Re: Looking for a VB Collections style example in C#
I think ArrayList is index based.
--
Jonathan Allen
"Conrad Fripp" <conrad.fripp@bmcnews.com> wrote in message
news:C#XbFC364388730@rp.csharpindex.com...
> Hi,
>
> If any C# gurus out there know how to implement a collection that is
VB-styled, please show me how.
>
> Methods wanted:
> <objectcollection>.Add (object) -- add object to collection
> <objectcollection>.Remove (index) -- remove object by index
> <objectcollection>.Item(index) -- to return an object by index
> <objectcolelction>.Count()
>
> Additionally, it would be nice to iterate through it using 'foreach'.
> The basic collection types seem OK to me with the exception that it
appears quite difficult to get one to easily return an item by it's index
value - this is the problem - doing it by key or object comparison seems
well supported. In my particular scenario, the collection is a group of
objects of the same type. I would like to know alot more about collections
and collection classes in C#. Any help is seriously appreciated.
>
> Many thanks,
>
> Conrad
>
> --
> Sent via http://csharpindex.com
> C# Index to resources
>
>
-
Re: Looking for a VB Collections style example in C#
eh??
I think all of the collections classes in .net work that way. Someone said
ArrayList is by index. That uses an indexer. So its really simple.
All the others have .GetByIndex if you wish to iterate through by index.
-
Re: Looking for a VB Collections style example in C#
I have done some looking around.
I had tried using an ArrayList, but it does not allow simple retrieval based on index. The .GetByIndex is only part of the SortedList type, which looks as though it may fit my criteria anyway. It is the ONLY COLLECECTION class which supports the .GetByInde
x, the other will require iteration from the beginning of a collection until the desired index is reached.
There is no method to determine the index number without calling .IndexOf and passing <collectionType>.GetEnumerator().Current as the input parameter, so it is easier to set up a variable to count as the collection is stepped through with the enumerator, w
hich also saves on repeated calls to the .IndexOf method and should save some processing time.
GJ wrote:
>
> eh??
>
> I think all of the collections classes in .net work that way. Someone said
> ArrayList is by index. That uses an indexer. So its really simple.
>
> All the others have .GetByIndex if you wish to iterate through by index
--
Sent via http://csharpindex.com
C# Index to resources
-
Re: Looking for a VB Collections style example in C#
"I had tried using an ArrayList, but it does not allow simple retrieval
based on index."
Ummm... yes it does. Straight from the documentation:
ArrayList.Item Property
Gets or sets the element at the specified index.
In C#, this property is the indexer for the ArrayList class.
Ryan LaNeve
"Conrad Fripp" <conrad.fripp@bmcnews.com> wrote in message
news:C#WEYe364474778@q3.csharpindex.com...
> I have done some looking around.
>
> I had tried using an ArrayList, but it does not allow simple retrieval
based on index. The .GetByIndex is only part of the SortedList type, which
looks as though it may fit my criteria anyway. It is the ONLY COLLECECTION
class which supports the .GetByIndex, the other will require iteration from
the beginning of a collection until the desired index is reached.
>
> There is no method to determine the index number without calling .IndexOf
and passing <collectionType>.GetEnumerator().Current as the input parameter,
so it is easier to set up a variable to count as the collection is stepped
through with the enumerator, which also saves on repeated calls to the
..IndexOf method and should save some processing time.
>
> GJ wrote:
> >
> > eh??
> >
> > I think all of the collections classes in .net work that way. Someone
said
> > ArrayList is by index. That uses an indexer. So its really simple.
> >
> > All the others have .GetByIndex if you wish to iterate through by index
>
> --
> Sent via http://csharpindex.com
> C# Index to resources
>
>
-
Re: Looking for a VB Collections style example in C#
I checked this out and your right, the documentation does say there is a .Item property. <B>But</B>, I hadn't found one when looking through the list members. It turns out it isn't there because it isn't actually defined for an ArrayList. I'm using VS b2 s
o either Microsoft haven't implemented it yet or the documentation is wrong.
If you create a class which defines:
<I>private ArrayList arrTest;</I>
and initialize it in the class constructor:
<I>arrTest=new ArrayList();</I>
and later try to do something like return an item as part of a function:
<I>return arrTest.Item(1);</I>
you will get the following compilation error:
<I>'System.Collections.ArrayList' does not contain a definition for 'Item'</I>
Try it yourself. I personally stick with what is exposed in the list members because this is what is implemented. Reading the documentation can make you think things which aren't true if the documentation is wrong - as it is here.
GJ wrote a reply saying I could use an indexer ArrayList[int Index] instead, which is true and didn't occur to me because I have been coding VB for too long.
Ryan LaNeve wrote:
>
> "I had tried using an ArrayList, but it does not allow simple retrieval
> based on index."
> Ummm... yes it does. Straight from the documentation:
>
> ArrayList.Item Property
> Gets or sets the element at the specified index.
> In C#, this property is the indexer for the ArrayList class.
>
> Ryan LaNeve
>
> "Conrad Fripp" <conrad.fripp@bmcnews.com> wrote in message
> news:C#WEYe364474778@q3.csharpindex.com...
> > I have done some looking around.
> >
> > I had tried using an ArrayList, but it does not allow simple retrieval
> based on index. The .GetByIndex is only part of the SortedList type, which
> looks as though it may fit my criteria anyway. It is the ONLY COLLECECTION
> class which supports the .GetByIndex, the other will require iteration from
> the beginning of a collection until the desired index is reached.
> >
> > There is no method to determine the index number without calling .IndexOf
> and passing <collectionType>.GetEnumerator().Current as the input parameter,
> so it is easier to set up a variable to count as the collection is stepped
> through with the enumerator, which also saves on repeated calls to the
> ..IndexOf method and should save some processing time.
> >
> > GJ wrote:
> > >
> > > eh??
> > >
> > > I think all of the collections classes in .net work that way. Someone
> said
> > > ArrayList is by index. That uses an indexer. So its really simple.
> > >
> > > All the others have .GetByIndex if you wish to iterate through by index
> >
> > --
> > Sent via http://csharpindex.com
> > C# Index to resources
> >
> >
--
Sent via http://csharpindex.com
C# Index to resources
-
Re: Looking for a VB Collections style example in C#
Ryan is correct, but you're missing a key bit of info.
Indexers in C# are used the same way arrays are.
So, for your code, you want something like:
ArrayList arrTest = new ArrayList();
arrTest.Add("Hello");
arrTest.Add("There");
return arrTest[0]; // returns "Hello"
"Conrad Fripp" <conrad.fripp@bmcnews.com> wrote in message
news:C#CbEG364819594@jc.csharpindex.com...
> I checked this out and your right, the documentation does say there is a
..Item property. <B>But</B>, I hadn't found one when looking through the list
members. It turns out it isn't there because it isn't actually defined for
an ArrayList. I'm using VS b2 so either Microsoft haven't implemented it yet
or the documentation is wrong.
>
> If you create a class which defines:
> <I>private ArrayList arrTest;</I>
>
> and initialize it in the class constructor:
> <I>arrTest=new ArrayList();</I>
>
> and later try to do something like return an item as part of a function:
> <I>return arrTest.Item(1);</I>
>
> you will get the following compilation error:
> <I>'System.Collections.ArrayList' does not contain a definition for
'Item'</I>
>
> Try it yourself. I personally stick with what is exposed in the list
members because this is what is implemented. Reading the documentation can
make you think things which aren't true if the documentation is wrong - as
it is here.
> GJ wrote a reply saying I could use an indexer ArrayList[int Index]
instead, which is true and didn't occur to me because I have been coding VB
for too long.
>
> Ryan LaNeve wrote:
> >
> > "I had tried using an ArrayList, but it does not allow simple retrieval
> > based on index."
> > Ummm... yes it does. Straight from the documentation:
> >
> > ArrayList.Item Property
> > Gets or sets the element at the specified index.
> > In C#, this property is the indexer for the ArrayList class.
> >
> > Ryan LaNeve
> >
> > "Conrad Fripp" <conrad.fripp@bmcnews.com> wrote in message
> > news:C#WEYe364474778@q3.csharpindex.com...
> > > I have done some looking around.
> > >
> > > I had tried using an ArrayList, but it does not allow simple retrieval
> > based on index. The .GetByIndex is only part of the SortedList type,
which
> > looks as though it may fit my criteria anyway. It is the ONLY
COLLECECTION
> > class which supports the .GetByIndex, the other will require iteration
from
> > the beginning of a collection until the desired index is reached.
> > >
> > > There is no method to determine the index number without calling
..IndexOf
> > and passing <collectionType>.GetEnumerator().Current as the input
parameter,
> > so it is easier to set up a variable to count as the collection is
stepped
> > through with the enumerator, which also saves on repeated calls to the
> > ..IndexOf method and should save some processing time.
> > >
> > > GJ wrote:
> > > >
> > > > eh??
> > > >
> > > > I think all of the collections classes in .net work that way.
Someone
> > said
> > > > ArrayList is by index. That uses an indexer. So its really simple.
> > > >
> > > > All the others have .GetByIndex if you wish to iterate through by
index
> > >
> > > --
> > > Sent via http://csharpindex.com
> > > C# Index to resources
> > >
> > >
>
> --
> Sent via http://csharpindex.com
> C# Index to resources
>
>
-
Re: Looking for a VB Collections style example in C#
Hey Conrad,
I use the CollectionBase class. Here is a quick example of how I get the
index. (Normally I store it as a property of the Class the collection is
using.)
using System;
using System.Collections;
public class CDemo
{
public int Index;
public CParentClass Parent;
public CDemo()
{
}
public CDemo(CParentClass p_Parent)
{
Parent = p_Parent;
}
}
public class CDemoCollection : CollectionBase
{
public CDemo this[int p_iIndex]
{
get
{
return (CDemo)List[p_iIndex];
}
set
{
List[p_iIndex] = value;
}
}
public CDemo Add(CDemo value)
{
int iIndex;
iIndex = List.Add((object)value);
CDemo DemoNew = (CDemo)List[iIndex];
DemoNew.Index = iIndex;
return DemoNew;
}
public CDemo Add(CParentClass p_Parent)
{
CDemo DemoNew = new CDemo(p_Parent);
return Add(DemoNew);
}
}
This allows me to access the Index by the value in the Index property of
CDemo. I am not 100% sure this will work right now, I am not sure if the
Index changes when I remove an item from the collection, but if so, you can
always just override the Remove and put a little code in it to reassign the
Index of each.
This also lets you use foreach().
The_Skipster
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks