By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Arraylist - accessing object properties?

P: n/a

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?

Oct 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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?
Alex <me******@test.comwrote in
news:Xn*****************************@199.45.49.11:
>
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?

Oct 20 '06 #2

P: n/a
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" wrote:
>
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?
Alex <me******@test.comwrote in
news:Xn*****************************@199.45.49.11:

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?

Oct 20 '06 #3

P: n/a
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
Alex wrote:
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?
Alex <me******@test.comwrote in
news:Xn*****************************@199.45.49.11:

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?
Oct 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.