Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1885
  • Last Modified:

vb.net :arraylist

I want to create a 2D array for example like this
|1 , item2 , 5|
|3,item22,  5|
|4,item222,7|
Can I use an arraylist..directly
Or I need to create arrays and then add those arrays to arraylist?
How to add and retrieve elements from arraylist ?
0
rishaan
Asked:
rishaan
1 Solution
 
Arthur_WoodCommented:
create a custom class, which exposes the three properties that you have for each entry, create instance of that class, and add those instances to the ArrayList.

what are actually stored in the arraylist are OBJECTS so, upon retrieving an arraylist memebr, you will need to cast it back as an instance of your custome class (using the cType function).

Public Class cMySpecialClass
   Private m_ID as Integer
   Private m_Name as String
   Private m_Count as Integer
   Public Property ID() as Integer
      Get
        Return m_ID
      End Get
      Set(ByVal RHS as Integer)
         m_ID = RHS
      End Set
   End Property
   Public Property Name() as String
      Get
        Return m_Name
      End Get
      Set(ByVal RHS as String)
         m_Name = RHS
      End Set
   End Property
   Public Property Count() as Integer
      Get
        Return m_Count
      End Get
      Set(ByVal RHS as Integer)
         m_Count  = RHS
      End Set
   End Property
End Class

then you would use code like this to add items to your arraylist:
 
Private m_arr as New ArrayList  ' this is either form level or global

Private m_SpecialObject as cMySpecialClass ' this is local to the proceudre where you are using it

m_SpecialObject  = New cMySpecialClass
with m_SpecialObject
    .ID = 1
    .Name = "item2"
    .Count = 5
    m_arr.Add m_SpecialObject
end with

m_SpecialObject  = New cMySpecialClass
with m_SpecialObject
    .ID = 3
    .Name = "item22"
    .Count = 5
    m_arr.Add m_SpecialObject
end with

m_SpecialObject  = New cMySpecialClass
with m_SpecialObject
    .ID = 4
    .Name = "item222"
    .Count = 7
    m_arr.Add m_SpecialObject
end with

this will add the three objects to the arraylist.

later when you want to retieve an itme from the arraylist


Dim objSpecial as cMySpecialClass

objSpecial = cType(m_arr.Item(index),cMySpecialClass)

AW
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now