How do I retreive a custom object's properties from an arraylist

R

Ric

Please forgive me if i dont explain this situation correctly. i'll try
to do my best.

because the arraylist.add method only accepts one argument, i created
an object with several public properties.

AddElement.vb (compiled and saved in Bin folder)
' VB Document
Imports System

Namespace myArrayObject

Public Class AddElement
Private _pName as String
Private _pType as String
Private _pSize as Integer

Public Property pName as String
Get
Return _pName
End Get
Set
_pName = Value
End Set
End Property

Public Property pType as String
Get
Return _pType
End Get
Set
_pType = Value
End Set
End Property

Public Property pSize as Integer
Get
Return _pSize
End Get
Set
_pSize = Value
End Set
End Property

End Class
End Namespace

Then I imported this object into a codebehind file. my goal was to
instiate the object's properties and set them accordingly

dbArraylist.vb (codebehind file with command event sub)

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HTMLControls
Imports myArrayObject
Imports System.Collections
Imports System.Data
Imports System.Data.SqlClient
Imports dbComponent

public class myALComponent
Inherits Page
Protected WithEvents lkID as linkbutton, lblAnswer as label
sub getActivityList(s as Object, e as CommandEventArgs)

Dim ID As Int32 = Int32.Parse(e.CommandArgument)

Dim myArrayList as new ArrayList, myParameter as new AddElement,
objItem as object
myParameter.pName = "@ID"
myParameter.pType = "ID"
myParameter.pSize = 0
myArrayList.add(myParameter)

myParameter.pName = "@ID2"
myParameter.pType = "ID2"
myParameter.pSize = 2
myArrayList.add(myParameter)

response.write("<br>myArrayList Count : "& myArrayList.count &
"<br>")

for each objItem in myArrayList
response.write("<br>AL : <br><li>" & objItem.toString)
next
end sub
end class

the aspx file passes the commandName and commandargument (= 1) via a
linkbutton. ive tested the link and it works, but im not using the
commandargument yet. my obstacle now is retreiving the property values
stored inside the object inside the arraylist. right now, i get
'myArrayObject.AddElement' as the objItem in myArrayList. that makes
sense because that is the object inside the arraylist. but, i want to
get the pName(string), pType(string) and pSize(integer) which are
public properties of the AddElement object from the
myArrayList(elementNumber). i tried casting it toString (result :
myArrayObject.Addelement) and cType(myArrayList(0), string) result :
can not cast error. thx for the help in advance.

ric
 
M

Martin Dechev

Hi, Ric,

Try to cast the elements of the ArrayList to the type of your class, i.e.

Response.Write(CType(myArrayList(0), AddElement).pName)

Hope thid helps
Martin
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top