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

My Google-Fu is weak today, hopefully this is a simple thing. I need to
This should hopefully be a simple one. I would like to add an extension
Hopefully this is simple: I have a relatively long list where each list item
Hopefully this is a simple one, but can anyone provide some simple c# code
Hopefully this is a quick question. I'm making a simple rock paper scissors game.
im struggling with syntax here: hopefully this question is v simple, im just miising
New to MVC, so hopefully this will be pretty simple. I have an HTML
hopefully a pretty simple question this time. I have a Select method in a
This should hopefully be a simple one. When using a date time picker in
This is hopefully a simple question: I have an OpenGL texture and would like

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.