Solved

Updating objects stored in an arraylist

Posted on 2004-08-20
4
303 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

617 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