Solved

Updating objects stored in an arraylist

Posted on 2004-08-20
4
272 Views
Last Modified: 2008-02-01
I am using an arraylist to store a collection of structure objects.

Each structure object consists of strings.

I can store and read the member strings of each object stored in the arraylist, but I can't figure out how to update a member string.

This is what I'm doing now:

    Private Sub cboQ1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboQ1.SelectedIndexChanged

        Dim s As String
        s = cboQ1.SelectedItem

        Dim c As Contact
        c = contactArray(arrayIndex)
        c.Q1 = s

    End Sub

As I said, I can access any object in the contactArray arraylist and read it, but when I assign a new value to an object's member, it doesn't stick - the next time I read the object from the arraylist, the change has disappeared.

(FWIW: Both contactArray and arrayIndex are declared as public:

    Dim contactArray As New ArrayList()

    Dim arrayIndex As Integer = 0)



Is there a way to update an object's items or do I need to remove the object from the arraylist, make the changes, and then insert/add the updated object back to the arraylist?

Thanks!

Jim
0
Comment
Question by:jacoulter
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11851283
You should be able to update the member strings in your contacts classes while they are in the ArrayList.

Can you show us the Contact class?

Idle_Mind
0
 

Author Comment

by:jacoulter
ID: 11851687
Public Structure Contact

    'declare structure members
    Private _lastName As String
    Private _firstName As String
    Private _street As String
    Private _city As String
    Private _state As String
    Private _zip As String
    Private _phone As String
    Private _q1 As String

    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal Value As String)
            _lastName = Value
        End Set
    End Property

    <snip>

    Public Property Q1() As String
        Get
            Return _q1
        End Get
        Set(ByVal Value As String)
            _q1 = Value
        End Set
    End Property

End Structure
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 11851917
Ok.  I see know.

You are using a Structure, not a Class.  You cannot reference a structure with another variable and modify it.  When you make one structure variable equal to another, it is simply copied, not referenced.  As such, modifications to one are not seen in the other.

You need to change your structure to a class for it to work.  Just change the word structure to class.

Here is some info on Structures from the help file:
Structures are similar to classes in that they represent data structures that can contain data members and function members. Unlike classes, however, structures are value types and do not require heap allocation. A variable of a structure type directly contains the data of the structure, whereas a variable of a class type contains a reference to the data, the latter known as an object. A structure inherits from System.ValueType and may not be inherited from.

In the case of classes, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. With structures, the variables each have their own copy of the data, so it is not possible for operations on one to affect the other, as the following example illustrates:

Structure Point
   Public x, y As Integer
   Public Sub New(x As Integer, y As Integer)
      Me.x = x
      Me.y = y
   End Sub
End Structure
Given the above declaration the following code fragment outputs the value 10:

Point a = new Point(10, 10)
Point b = a
a.x = 100
Console.WriteLine(b.x)
The assignment of a to b creates a copy of the value, and b is thus unaffected by the assignment to a.x. Had Point instead been declared as a class, the output would be 100 because a and b would reference the same object.

Regards,

Idle_Mind
0
 

Author Comment

by:jacoulter
ID: 11852074
Changing from structure to class worked - thanks very much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now