Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3602930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:48:31+00:00 2026-05-18T20:48:31+00:00

My program has decided to assign pointers instead of make copies of an object,

  • 0

My program has decided to assign pointers instead of make copies of an object, and I’m not sure why. I have something like this:

    Public Class Foo
       Private myFooData As New List(Of FooData)

       Public Sub New(ByVal newFooData As List(Of FooData))
           myFooData = newFooData 
       End Sub

       Public Property FooValues() As List(Of FooData)
           Get
               Return myFooData 
           End Get
           Set(ByVal value As List(Of FooData))
                   myFooData = value
            End Set
       End Property
    End Class

And it’s used like this:

    Public Sub Dosomething()
        Dim mainFoo as new Foo

        For x = 1 to 10
            mainFoo.FooValues(x) = New FooData
        Next

        Dim originalFoo as new Foo
        originalFoo.FooValues = mainFoo.FooValues.Take(3).ToList

        Dim newFoo as new Foo
        newFoo.FooValues = originalFoo.FooValues

        newFoo.FooValues(1) += 1
    End Sub

Very simplified, but basically what I’m doing. So for some reason today when I change item in newFoo.FooValues, originalFoo.FooValues also changes, and mainFoo does not. I’ve tried assigning the entire objects as well and I get the same results. Any ideas why this may be happening and how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:48:31+00:00Added an answer on May 18, 2026 at 8:48 pm

    This is how assignment in .Net is supposed to work.

    When you called .ToList() in the middle of your second snippet, your code iterates over the set and makes copies into a whole new list. This is why your mainFoo object is “protected” — you created a new instance. If the FooData items being copied are themselves references to objects (hint: they probably are), then only the references are copied. The only exceptions are for strings and value types (primitives and structures), or if you code it by hand.

    It’s usually a good idea for List properties to make the property readonly:

    Public Property FooValues() As List(Of FooData)
       Get
           Return myFooData 
       End Get
    End Property
    

    This will still let you manipulate the list to your heart’s content, but prevents you from completely switching a list instance out from under the class. The same is true for other complex types exposed from a class as a property.

    The one thing you don’t want to do is change this to be a Structure instead of a Class. This may seem to do what you want at first, but it will cause other problems for you later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my problem. I have a program that has to run in a
I have an application that seems to throw exceptions only after the program has
I have a program which has some Textareas / Labels these can be anywhere
I'm making a program that has a lot of constants. I decided to put
I have an existing program that has its own main loop, and does computations
Whenever I exit my program it gives me this exception 0xC0000022: A process has
My program has to read files that use various encodings. They may be ANSI,
What do I need to do to save an image my program has generated
The target language is C/C++ and the program has only to work on Linux,
I am writing a program which has two panes (via CSplitter ), however I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.