Arraylist - accessing object properties?

A

Alex

Hi all,

I'm trying to create an arraylist of a user control class... I'm able to
define the list and add objects (panels) to it, but I can access and of the
panel properties using an index...

Is there a way to do this, or do I need to copy each node into an interim
handle of the correct class before manipulating it?

ie,

I can do this:

UC_TeamMemberPanel p1 = new UC_TeamMemberPanel();
p1.Name = "Panel " + counter;

....to change the Name property. However, when I do this:

ArrayList teamMembers = new ArrayList();
teamMembers.Add(new UC_TeamMemberPanel());

I cannot access the name property in this way:

teamMembers[0].Name = "Panel " + counter;

Is there a way to access the .Name prop via the array?
 
A

Alex

Got it, I think...

This seems to work:

UC_TeamMemberPanel p = (UC_TeamMemberPanel)teamMembers[0];
p.Name = "Panel " + counter;


Guess I have to do then whenever accessing a node in the array?
 
G

Guest

If you're using C# 2, a far better approach is List<T>.
e.g.,
List<UC_TeamMemberPanel> = new List<UC_TeamMemberPanel>();
(need a "using System.Collections.Generic")
You can then add to it just like an ArrayList, but when reading from it, no
intermediate casting step is necessary.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


Alex said:
Got it, I think...

This seems to work:

UC_TeamMemberPanel p = (UC_TeamMemberPanel)teamMembers[0];
p.Name = "Panel " + counter;


Guess I have to do then whenever accessing a node in the array?


Hi all,

I'm trying to create an arraylist of a user control class... I'm able
to define the list and add objects (panels) to it, but I can access
and of the panel properties using an index...

Is there a way to do this, or do I need to copy each node into an
interim handle of the correct class before manipulating it?

ie,

I can do this:

UC_TeamMemberPanel p1 = new UC_TeamMemberPanel();
p1.Name = "Panel " + counter;

...to change the Name property. However, when I do this:

ArrayList teamMembers = new ArrayList();
teamMembers.Add(new UC_TeamMemberPanel());

I cannot access the name property in this way:

teamMembers[0].Name = "Panel " + counter;

Is there a way to access the .Name prop via the array?
 
C

Cryptik

Yes it's a casting issue, you need to cast the value in the array to
the proper type so the system knows that property exists. You could get
around it by making your own ArrayList class that returns the type your
looking for in the indexer, or by using generics in .NET v2 like
List<UC_TeamMemberPanel> .

Kelly S. Elias
Webmaster
DevDistrict - C# Code Library
http://devdistrict.com

Got it, I think...

This seems to work:

UC_TeamMemberPanel p = (UC_TeamMemberPanel)teamMembers[0];
p.Name = "Panel " + counter;


Guess I have to do then whenever accessing a node in the array?


Hi all,

I'm trying to create an arraylist of a user control class... I'm able
to define the list and add objects (panels) to it, but I can access
and of the panel properties using an index...

Is there a way to do this, or do I need to copy each node into an
interim handle of the correct class before manipulating it?

ie,

I can do this:

UC_TeamMemberPanel p1 = new UC_TeamMemberPanel();
p1.Name = "Panel " + counter;

...to change the Name property. However, when I do this:

ArrayList teamMembers = new ArrayList();
teamMembers.Add(new UC_TeamMemberPanel());

I cannot access the name property in this way:

teamMembers[0].Name = "Panel " + counter;

Is there a way to access the .Name prop via the array?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top