#1
  1. Standing 3 feet to the left.
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2006
    Location
    USA
    Posts
    1,435
    Rep Power
    185

    [SOLVED][VB.NET] Setting Property that is part of list sets all properties


    I have a list of vendors, each vendor can have multiple addresses (office, store, etc.) Each vendor has at least one address, which is their primary address, and this is stored as the first (0) record.

    I have created a vendor class, and each vendor item has a List(Of Address), where Address is another custom class. A small portion of the Address class is actually put to use in the Vendor class and accessed from the outside.

    My problem is that whenever I try to set a property on this list, it sets all properties on all items on the list (at least, as much as it can). I'm assuming it's some sort of reference issue, but hell if I know what would do this.

    Portion of MainClass:
    VB.NET Code:
            Dim oVendor As New Vendor
    '...
            With oVendor
                .Country = txtCountry.Text
                .Address1 = txtAddress1.Text
    ...

    Portion of Vendor:
    VB.NET Code:
        Dim Addresses As List(Of Address)
     
    Public Sub New()
    ...
            Addresses = New List(Of Address)
            AddAddress()
        End Sub
     
        Public Sub AddAddress()
            Addresses.Add(New Address())
        End Sub
     
        Public Property Country As String
            Get
                Country = Addresses(0).Country
            End Get
            Set(ByVal Text As String)
                Addresses.Item(0).Country = Text
            End Set
        End Property
     
        Public Property Address1 As String
            Get
                Address1 = Addresses(0).AddressLine1
            End Get
            Set(ByVal value As String)
                Addresses.Item(0).AddressLine1 = value
            End Set
        End Property
    Setting .Country also sets .Address1 as well as every other property in the Address class it can (not just those that I've created properties for in Vendor). Setting .Address1 sets .Country and every other Address property.

    Please help; I have another class that uses List() in the exact same way and does everything the same and works fine, so this is making me consider running away to a monastery. :whyistherenocryingsmilie:

    EDIT: I should note that all non-list properties store correctly on the Vendor class.

    EDIT 2: Attempt to change the List to a straight-up Array had the same issues.
    Last edited by Tukaro; February 6th, 2012 at 03:50 PM.
    I'd rather teach you to fish than give you a fish. I reserve the right to also slap you with the fish.
  2. #2
  3. Standing 3 feet to the left.
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2006
    Location
    USA
    Posts
    1,435
    Rep Power
    185
    I've solved my problem. The Address class was based off a COM object; despite having all the right properties, the COM object didn't bother to actually do anything with properties until I had initialized a file connection; I wasn't trying to modify a file with this, just use it as a structure since it was already set up.

    But apparently either the COM or VisualStudio (probably the COM!) was happy to let me set properties that didn't actually exist, and then pretend all properties were that value when I tried to recall them.

    And so goes basically my entire day thus far. Ugh.
    I'd rather teach you to fish than give you a fish. I reserve the right to also slap you with the fish.

IMN logo majestic logo threadwatch logo seochat tools logo