Solved

Updating objects stored in an arraylist

Posted on 2004-08-20
4
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

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 86

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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