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 6026979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:31:24+00:00 2026-05-23T04:31:24+00:00

Hopefully this is simple enough. I need to be able to add elements to

  • 0

Hopefully this is simple enough. I need to be able to add elements to a copy of mylist.testlist without modifying the global mylist object. (Which seems to be happening via the below code.)

When I am working on x, which should be a totally separate object, mylist is getting modified as well. How can I fix this? I have worked with lists extensively and never seen this behavior before. I have tested and reproduced the problem in .NET 3.5 and .NET 4.0 on Win 7 Pro 32bit.

TIA!

Source Code:

Public Class Form1
    Public mylist As New test

    Sub Main()
        Dim x As test = mylist
        For i As Integer = 0 To 10
            x.testlist.Add(False)
        Next

        MsgBox("x count: " + x.testlist.Count.ToString + vbCrLf + "mylist count: " + mylist.testlist.Count.ToString)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Main()
    End Sub
End Class
Public Class test
    Public testlist As New List(Of Boolean)
    Public Sub New()
        testlist.Add(False)
    End Sub

End Class
  • 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-23T04:31:25+00:00Added an answer on May 23, 2026 at 4:31 am

    As Tim Schmelter pointed out in his comment, your problem is the line:

    Dim x As test = mylist
    

    by which x gets a reference to mylist. That means both (x and mylist) are pointing to the same instance of test. That’s why changing one of them is changing the other too.

    To fix it, you could define x as a new instance of test and copy all elements from mylist.testlist to x.testlist.

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

Sidebar

Related Questions

hopefully this is a simple question. I have a simple custom forum on my
Hey, This is just a simple exercise from class that I decided to have
just a quick question to hopefully get pointed in the right direction. I took
This is my first question after having used this place as my go to
I have approached you today to try and gather your opinions and experience with
I'm tracking down a pesky problem and I've narrowed down the problem down and
We have a collection of objects which grows quite large over time. We have
I'm just learning Oracle and I understand the concept of triggers and sequences. What
I am looking for a way to modify font types in ggplot. At the
Today I was working on an old diagnostic library I had written back in

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.