ArrayList usage

ArrayList usage

Post by Jim Owe » Fri, 14 Mar 2003 04:04:19



I am having some trouble understanding how to use ArrayLists. The code below
gives me an error saying
"This range in the underlying list is invalid. A possible cause is that
elements were removed." on the last line of the example. Not only that, but
when I change al2[0] to "Hello", al3[0] is changed as well ,as is al1[0],
meaning GetRange copies pointers only. What I want to do here is stuff al1
and al2 with a copy of the list in al1. In subsequent code, al2 and al3 will
be manipulated, but they need to start at the same point. How do I do that?

al2 = al1.GetRange(0, al1.Count);
al3 = al1.GetRange(0, al1.Count);
al2[0] = "Hello";
string x = al3[0].ToString();

Much thanks in advance,
--
- Jim Owen
206-501-6936

 
 
 

ArrayList usage

Post by John Saunder » Fri, 14 Mar 2003 06:10:39


From the documentation for ArrayList.GetRange:

-------------------
Remarks

This method does not create copies of the elements. The new ArrayList is
only a view window into the source ArrayList. However, all subsequent
changes to the source ArrayList must be done through this view window
ArrayList. If changes are made directly to the source ArrayList, the view
window ArrayList is invalidated and any operations on it will return an
InvalidOperationException.
-------------------

You modified it through al2, then accessed it through al3. Did you get an
InvalidOperationException?
--
John Saunders
Internet Engineer


Quote:> I am having some trouble understanding how to use ArrayLists. The code
below
> gives me an error saying
> "This range in the underlying list is invalid. A possible cause is that
> elements were removed." on the last line of the example. Not only that,
but
> when I change al2[0] to "Hello", al3[0] is changed as well ,as is al1[0],
> meaning GetRange copies pointers only. What I want to do here is stuff al1
> and al2 with a copy of the list in al1. In subsequent code, al2 and al3
will
> be manipulated, but they need to start at the same point. How do I do
that?

> al2 = al1.GetRange(0, al1.Count);
> al3 = al1.GetRange(0, al1.Count);
> al2[0] = "Hello";
> string x = al3[0].ToString();

> Much thanks in advance,
> --
> - Jim Owen
> 206-501-6936


 
 
 

ArrayList usage

Post by Jim Owe » Fri, 14 Mar 2003 07:33:27


Well, OK, so that's what I'm doing wrong. I don't know where you found that
documentation. Anyway, so how SHOULD I be copying these elemnts into al2 and
al3 then, if not using GetRange?

--
- Jim Owen
206-501-6936

> From the documentation for ArrayList.GetRange:

> -------------------
> Remarks

> This method does not create copies of the elements. The new ArrayList is
> only a view window into the source ArrayList. However, all subsequent
> changes to the source ArrayList must be done through this view window
> ArrayList. If changes are made directly to the source ArrayList, the view
> window ArrayList is invalidated and any operations on it will return an
> InvalidOperationException.
> -------------------

> You modified it through al2, then accessed it through al3. Did you get an
> InvalidOperationException?
> --
> John Saunders
> Internet Engineer



> > I am having some trouble understanding how to use ArrayLists. The code
> below
> > gives me an error saying
> > "This range in the underlying list is invalid. A possible cause is that
> > elements were removed." on the last line of the example. Not only that,
> but
> > when I change al2[0] to "Hello", al3[0] is changed as well ,as is
al1[0],
> > meaning GetRange copies pointers only. What I want to do here is stuff
al1
> > and al2 with a copy of the list in al1. In subsequent code, al2 and al3
> will
> > be manipulated, but they need to start at the same point. How do I do
> that?

> > al2 = al1.GetRange(0, al1.Count);
> > al3 = al1.GetRange(0, al1.Count);
> > al2[0] = "Hello";
> > string x = al3[0].ToString();

> > Much thanks in advance,
> > --
> > - Jim Owen
> > 206-501-6936

 
 
 

ArrayList usage

Post by John Saunder » Fri, 14 Mar 2003 08:35:09


I just typed "ArrayList.GetRange" into the Index window in Visual
Studio.NET, since I wasn't familiar with GetRange (*) . I also found the
same article in the MSDN Library online:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre...
frlrfsystemcollectionsarraylistclassgetrangetopic.asp.

As to copying the elements, how about ArrayList.Clone()?

--
John Saunders
Internet Engineer

(*) Thanks for the opportunity to look this up. I also wasn't familiar with
Adapter, and I have an immediate use for that!


> Well, OK, so that's what I'm doing wrong. I don't know where you found
that
> documentation. Anyway, so how SHOULD I be copying these elemnts into al2
and
> al3 then, if not using GetRange?

> --
> - Jim Owen
> 206-501-6936


> > From the documentation for ArrayList.GetRange:

> > -------------------
> > Remarks

> > This method does not create copies of the elements. The new ArrayList is
> > only a view window into the source ArrayList. However, all subsequent
> > changes to the source ArrayList must be done through this view window
> > ArrayList. If changes are made directly to the source ArrayList, the
view
> > window ArrayList is invalidated and any operations on it will return an
> > InvalidOperationException.
> > -------------------

> > You modified it through al2, then accessed it through al3. Did you get
an
> > InvalidOperationException?
> > --
> > John Saunders
> > Internet Engineer



> > > I am having some trouble understanding how to use ArrayLists. The code
> > below
> > > gives me an error saying
> > > "This range in the underlying list is invalid. A possible cause is
that
> > > elements were removed." on the last line of the example. Not only
that,
> > but
> > > when I change al2[0] to "Hello", al3[0] is changed as well ,as is
> al1[0],
> > > meaning GetRange copies pointers only. What I want to do here is stuff
> al1
> > > and al2 with a copy of the list in al1. In subsequent code, al2 and
al3
> > will
> > > be manipulated, but they need to start at the same point. How do I do
> > that?

> > > al2 = al1.GetRange(0, al1.Count);
> > > al3 = al1.GetRange(0, al1.Count);
> > > al2[0] = "Hello";
> > > string x = al3[0].ToString();

> > > Much thanks in advance,
> > > --
> > > - Jim Owen
> > > 206-501-6936

 
 
 

1. ArrayList.Count won't decrease after calling ArrayList.RemoveAt()

Hello,

I'm writing a Windows Form control, and it's structure looked like this:

public class mycontrol : ListControl
{
        private ArrayList m_aItemList = new ArrayList();
        private ArrayList m_aItems = ArrayList.Synchronized(m_aItemList);

        public class ObjectCollections
        {
                private mycontrol m_container;
                private ArrayList m_aItems;

                public ObjectCollections(mycontrol ctrl)
                {
                        m_container = ctrl;
                        m_aItems = ArrayList.Synchronized(m_aItems);
                }

                private ObjectCollections m_aListItems = new
ObjectCollections(this);
        }

        public ObjectCollections Items
        {
                get { return m_aListItems;}
        }

After calling mycontrol.Items.RemoveAt(),  mycontrol.Items.Count remains all
the same
value.  Can you tell me where am I wrong?  Thanks in advance.

Regards,

Arthur

2. Win2k Installs CD drives wrongly!

3. Web Service Method "Invoke()" is Blocked by Idle CPU Usage

4. Useable FP 2000 sites?

5. .NET executable process high memory usage?

6. FORSALE: Complete CAD System

7. DataAdapter usage/design question

8. power problems

9. XML Designer usage for supporting attributes in Typed DataSets

10. ReadXML usage

11. ExecuteNonQuery usage

12. Efficient usage of SqlConnection

13. Memory Usage