Property of an object within an object

How do I access the property of an object that is within another object?  For example, I have an object consisting of an ArrayList of object.  The objects contained in the ArrayList have the properties DisplayText and ValueText.  What is the syntax to retrieve those properties?

Here's the class file code:
Public Class MenuOption
Public DisplayText As String
Public ValueText As String
Public Sub New(DisplayText As String, ValueText As String)
Me.DisplayText = DisplayText
Me.ValueText = ValueText
End Sub
End Class

Public Class StatesMenu
Private _colStates As ArrayList
Public Property colStates() As ArrayList
Return _colStates
End Get
Set(ByVal Value As ArrayList)
_colStates = Value
End Set
End Property
Public Sub New()
_colStates = New ArrayList
_colStates.Add(New MenuOption("ALABAMA","AL"))
_colStates.Add(New MenuOption("WYOMING","WY"))
End Sub
End Class

Then the page code:

Sub Page_Load
If NOT IsPostBack Then
Dim objStates As StatesMenu
objStates = New StatesMenu
dropState.DataSource = objStates.colStates
dropState.DataTextField = ???? 'the DisplayText property of MenuOption object
dropState.DataValueField = ???? 'the ValueText property of the MenuOption object
End If
End Sub

Or is this too convoluted of a way to go about this? I just want a way to populate dropdown menus that appear throughout the app. In this case, a 'States' dropdown, but I intent to expand this for a variety of different dropdowns.
2/11/2004 4:57:09 PM 91979 articles. 4 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 56

Well, you can cast it.

There are two ways to cast here is the faster one one
Dim x as MenuOption
_colStates.Add(New MenuOption("ALABAMA","AL"))
x = DirectCast(_colStates(0), MenuOption)
Here is the slower one
Dim x as MenuOption
_colStates.Add(New MenuOption("ALABAMA","AL"))
x = CType(_colStates(0), MenuOption)
2/11/2004 5:53:34 PM

DataTextField and DataValueField require a String, which indicates which FIELD (as their names imply!!) in a Collection should be bound to the appropriate property of the ListItem Controls. I don't know how you can possibly assign the values to the DropDownList in this manner.
I suggest scrapping this idea and simply extending the DropDownList and inserting the States in the CreateChildControls method. The MenuOptions are being hardcoded anyway, so this way there is no need to DataBind the values. For example:
    public class StateDropDown : System.Web.UI.WebControls.DropDownList {

protected override void CreateChildControls () {
Controls.Add ( new ListItem ( "ALABAMA", "AL" ) );
Controls.Add ( new ListItem ( "WYOMING", "WY" ) );

Steven Bey

Recursion: see Recursion
2/11/2004 6:11:05 PM
this is a little late but i just wanted to note you could've used a hashtable for this.  But as stevenbey said there is no point to waht you were trying to do in the beginning.
6/10/2004 3:54:03 PM

Similar Artilces:

displaying property of object within an object on gridview
I have a business class defined as followsPublic Class AbbrPlant Private _ID As Integer Private _PlantName As String   Public Property ID() As Integer GetReturn _ID End GetSet(ByVal value As Integer) _ID = value End Set End Property Public Property PlantName() As String GetReturn _PlantName End GetSet(ByVal value As String) _PlantName = value End Set End Property Public Site As New Site End Class Public Class PlantList Inherits List(Of AbbrPlant)Public Sub New() End Sub End Class   Site is a class that is an address class with properties AddressLin...

detailsview and objects (and objects inside objects inside objects)
hi there!as I was browsing through the posts related to the detailsview I couldn't find someone with the problem I'm facing so I decided to post here...well, I was able to bound an object datasource into my gridview and detailsview, it deletes and shows the data very well, BUT inside the object, there's another object, for example, I have bound a class called Province and inside the Province class there's the Country class indicating the data from the related country, to edit and/or insert I have placed a drop down list so the user selects the country from a drop down list, everything seems ...

I just started getting this error in vs2005b2: Object reference not set to an instance of an object.
All I have to do is create a new web project, create a master page, then create a normal web page.  I read a posting someplace else that suggested removing the user profile solves this problem. So, if that is the case, how to I remove my user profile?  Peter Kellner and blogging athttp://PeterKellner.netMVP, ASP.NET Can you post more information about the error? (Line number, source code found at file/line of error, etc.)? Also, you can try running the same page in the browser directly, and see what error ASP.NET gives you. Thanks,Shanku Niyogi Th...

Property returns "Object" object that contains custom objects
Hi, I had some difficulties on returning the property of a custom JavaScript Object. Assuming I had a JSObject ObjA that was created using JS_NewObject with my own JSClass ObjA_class. And ObjA_class has one property getObjBs that will return a JavaScript standard "Object" object. And I want this "Object" object has properties that names are same as each ObjB object's property "id" , and the value of each properties is ObjB object. e.g. There are 2 ObjB objects: student1 and student2. The ObjB has a propperty "id":

Creating objects within objects.
I have a friend who is trying to create a form on the fly with a pagecontrol, tabsheets and wants to add scrollbox and 3 images to each tabsheet. We cannot figure out why this does not work. Any idea's would be appreciated. Code is his but I have updated it with what little I know. ;-) Thanks. procedure TfGallery.btnBuildLayersClick(Sender: TObject); var im: TImage; ts: TTabSheet; sb: TScrollBox; i, j, iActLeft, iActTop : integer; begin //Create TabSheets & add Captions for i := 0 to 4 do begin ts := TTabSheet.Create(PageControl1); with ts do ...

The start of objects, in object.ops
I've decided part of the implementation for objects, and started in on the ops for them. Right now it's just a sketch, but the following ops are going in: newclass Px, Sy Creates a new, standalone class named Sy, class PMC put in Px. subclass Px, [SPy], [Sz] Subclass $2, and put the new subclass PMC in $1, If $3 is specified (it's optional) it'll be the name of the new subclass, otherwise an anonymous subclass is created. singleton Px Takes the object in Px and puts it in its own anonymous subclass of its current class. class Px, Py Gets the cl...

accessing objects within objects
I have a class, Computer. I have a hashtable full of classes that is within Computer. It is Categories (ie cpu, ram, etc). I try to access my Category like this and it doesn't work. I am recently switching from ASP to ASP.NET/C# so vbscript was my only previous programming experience. Computer myComputer = new Computer("tower2000"); // within the Computer constructor is when it creates the 30 or so categories // of computer parts, as a hashtable and creating a new Category object for each Category myCategory = (Category) myComputer.myCategories[0]; Label1.Text = myC...

Deserializing An Object from within that object
I have an C# Class I can easily serialize into XML and desereilalize it. However, I want to do this from Inside the object. Here's a sample of my code that I used from the Code-behind: string strPackageListPath = Path + "\\test.xml"; if (System.IO.File.Exists(strPackageListPath)) { System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(PackageList)); System.IO.TextReader tr = new System.IO.StreamReader(strPackageListPath); INSTANTIATED_OBJECT = (PackageList)s.Deserialize(tr); tr.Close(); } So, instead of using INSTANTIATED_OBJECT, It woul...

How to get all function-object properties from within jsd.enumerateScripts
In Firebug, I am trying to list all function object properties (defined on a page) when enumerating all scripts by jsd.enumerateScripts (jsd activated). Here is roughly what I am doing in the extension (tested with Firefox nigthly and Firefox 6) jsd.enumerateScripts( { enumerateScript: function(script) { var func = script.functionObject; var props = self.getProperties(func); sysout(props.join(", ")); } }); getProperties: function(scope) { var props = []; var listValue = {value: null}, lengthValue = {value: 0}; sc...

Getting an "Object reference not set to an instance of an object", and no idea which object
I'm working on one of our ASP.NET 2005 projects now.  For the longest time we've gotten an error when compiling it that says, "Object reference not set to an instance of an object".  However, the irritating thing is it doesn't say what "object" its having problems with, and it certainly isn't telling me which file that has the problem.  So, I'm getting this error message that doesn't tell me where the error comes from.  All I know is that the error is "Out there".  Reminds me of an old X-Files episode. How am I t...

How to Bind to property of inner object of an object?
Hi,     In my case, I have two objects like this: 1 public class Address{ 2 private string _country; 3 public string Country { get {return _country;} set {_country=value;}} 4 5 private string _zip; 6 public string Zip { get {return _zip;} set {_zip=value;}} 7 } 8 9 public class Person{ 10 private string _firstname; 11 public string FirstName{ get {return _firstname;} set {_firstname=value;}} 12 13 private string _lastname; 14 public string LastName{ get {return _lastname;} set {_lastname=value;}} 15 ...

force a Linq-To-Entities query to get all related objects when getting a parent object
All -- Please help. How can one force a Linq-To-Entities query to get all related objects when getting a parent object? Is there a way to set the depth of the query? Please advise. For an example, check below for the code...   public Team.DynamicData01.Business.Entities.TestCoreRecord GetEntity(long targetId){ Team.DynamicData01.Business.Entities.TestCoreRecord myEntity = null;  using (Team.DynamicData01.Business.Entities.NorthwindEntityManager myContext = new Team.DynamicData01.Business.Entities.NorthwindEntityManager()) {  var q = from...

objectdatasource and business objects with object properties
Hi Lets say i have a business object called Customer which has a property called Address which is itself and object of type Address (in this sense there is a one-to-one relationship between Customer and Address). Lets say customer has properties forename and surname and Address has properties add1 and postcode Could you use an objectdatasource to perform updates on a customer object? For example, if your objectdatasource had a method updateCustomer(Byval c as customer) and your gridview had fields forename and surname and  add1 and postcode, would the objectdatasource know to map fore...

Object to Object Initialization in
Hi, I want to Initialization the the base class object to derived class object. but it gives an error in c#.Net. It's works fine in VB.Net bt in dosn't work. what exactly i m doing as follows; class class1 { pubic int i; } class class2 : class1 { // some method .... } in the main function. static void main() { class1 c1 = new class1(); class c2; c2=c1; // this is  not workin in alougth it's woks fine in class c2 = new class1(); // i don't want  like this. is there any onther way in c# to acheive this. please help me.. }Sandeep...

Web resources about - Property of an object within an object -

Property - Wikipedia, the free encyclopedia
In abstraction, property is that which belongs to or with something, whether as an attribute or as a component of said thing. For the significant ...

Property - Wikipedia, the free encyclopedia
This article may require copy editing for grammar, style, cohesion, tone, or spelling . You can assist by editing it . In abstract, property ...

Chinese property firm Yuhu hires ex-deputy premier Andrew Stoner
A Chinese-based property firm that has donated hundreds of thousands of dollars to Australian political parties has added former deputy premier ...

Project Tetris hits commercial property market as federal government minimises costs
... The federal government's commitment to reducing its vacant office space across Canberra is having a direct impact on the commercial property ...

Mt Maunganui property home of the year - The Courier-Mail Search Search
A LUXURY beachside home in Mt Maunganui and a renovation in Auckland have won supreme awards at New Zealand's 25th Registered Master Builders ...

Treasurer Scott Morrison orders forced sale of $10m of property
Foreign investors who have bought established residential real estate without notification given until end of month to declare purchases.

Real estate: Couples opting for discount weddings to get a foot on property ladder
WHEN it comes to saving up for a home, life can get in the way.

Property estates on former Perth school sites popular with buyers - PerthNow Search Search
CRAIGIE, about 24km from the CBD, is the site of Perth’s latest residential school conversion. The first 22 blocks at the site of the former ...

Chinese property firm Yuhu hires ex-deputy premier Andrew Stoner
A Chinese-based property firm that has donated hundreds of thousands of dollars to Australian political parties has added former deputy premier ...

Esperance Bushfire: Reports of property losses as fires rage in south WA
Fast-moving, out-of-control bushfires threaten lives and homes in the Esperance region in southern Western Australia.

Resources last updated: 11/24/2015 2:34:14 AM