Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 28th, 2004, 01:10 PM
Registered User
 
Join Date: May 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to augustwest Send a message via MSN to augustwest Send a message via Yahoo to augustwest
Default Object properties in an ArrayList

I have a class that contains a private member variable in the form of an ArrayList. The purpose of the ArrayList is to store other objects. I am able to instantiate the object that containes the ArrayList, instantiate the object that I want to add and add it to the ArrayList, but cannot access the properties of the objects stored within the ArrayList.

This seems very possible since the best function I can see for an ArrayList is grouping objects of difference types, which is way cool in my opinion, but what is the point if I can't access the objects within? Though I am sure that I can.

If code samples of what I am doing are needed, please ask. Thanks!

UPDATE: Do I need to implement the IList interface on the objects that I store within the ArrayList?
 
Old May 28th, 2004, 01:51 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I think the problem you might be having is just a problem of object typing/conversion. Usually you have to do something like this:

((myobject)arraylist[0]).myobjectproperty

The arraylist doesn't know what the objects are specifically. They are all just objects (which is of course, every variable you can instantiate in .NET) so you need to convert before you can see the specific properties of the given object.

Peter
-------------------------
Work smarter, not harder
 
Old May 28th, 2004, 03:20 PM
Registered User
 
Join Date: May 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to augustwest Send a message via MSN to augustwest Send a message via Yahoo to augustwest
Default

Thanks, it's just to bad it won't let me do:
Code:
((arraylist[0].GetType())arraylist[0]).myobjectproperty
Then I could use a for loop to increase the index till arraylist.Count, but that doesn't seem to compile, any other ideas?


 
Old June 2nd, 2004, 10:56 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What you are referring to is late binding which you should try to avoid. If the objects in the array list you are dealing with are unknown, then you'll have to test each one against a list of expected types. You will obviously know what objects contain the member "myobjectproperty" so you could check each one. An alternative is to use objects that are derived from a single base type that supports that member. Then you can covert explicitly to that base type to access that member. It's not as easy to do on-the-fly conversion as it was in ASP. (That's why .net is so much faster.)
 
Old June 16th, 2004, 04:35 PM
tnd tnd is offline
Authorized User
 
Join Date: Jun 2004
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think it doesn't compile because
arraylist[0].GetType() returns 'object' type
and therefore won't compile because myobjectproperty is not part of 'object'

I believe late binding is not possible in .NET so you really only have 2 choices. 1) is to use a common base object as described by planoie. 2) you can code a custom array to return yourobject as the default indexer.






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to access parent object properties kaos_frack Javascript How-To 4 July 31st, 2007 04:31 AM
Object Properties & document.activeElement interrupt Javascript How-To 4 April 21st, 2005 03:49 PM
Extracting COM Object Properties walkins5 General .NET 0 February 5th, 2005 09:34 PM
Properties/Methods Not in Object Browser ritag VS.NET 2002/2003 0 January 13th, 2005 12:59 PM
How to set (right click)properties for an object mpkrishna Classic ASP Components 1 August 24th, 2003 01:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.