Improve company productivity with a Business Account.Sign Up

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

Updating objects stored in an arraylist

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
jacoulter
Asked:
jacoulter
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
jacoulterAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
jacoulterAuthor Commented:
Changing from structure to class worked - thanks very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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