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

  • Home
  • SEARCH
  • 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 6142223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:21:26+00:00 2026-05-23T18:21:26+00:00

Sorry for such a basic scoping question but I am obviously not understanding something

  • 0

Sorry for such a basic scoping question but I am obviously not understanding something about scoping that is very basic. I have a very simple class:

Public Class testListClass
    ' This just contains a single list that is set by a property or the constructor
    Private classArrayList As New ArrayList()

    Public Sub New(ByVal theList As ArrayList)
        classArrayList = theList
    End Sub
End Class

Then I have a block of code that instantiates this when I press a button passing a new testListClass object to it containing three values (1,2,3).

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    ' Lets see if changing the arrayList results in all of the testListClass items being changed
    Dim theList As New List(Of testListClass)
    Dim localArrayList As New ArrayList()

    localArrayList.Add(1)
    localArrayList.Add(2)
    localArrayList.Add(3)

    theList.Add(New testListClass(localArrayList))

    ' This results in theList.classArrayList being cleared.  Why since the parameter
    ' to the constructor is passed by value?
    localArrayList.Clear()   

    localArrayList.Add(10)
    localArrayList.Add(20)

    theList.Add(New testListClass(localArrayList))


End Sub

After the “theList.Add(New testListClass(localArrayList))” call, theList contains one “testListClass” object which contains three values (1,2,3) just as I would expect. The following is the what I don’t understand. The next call is:

localArrayList.Clear()  

If I set a breakpoint here in the debugger and execute this line what I see is:

theList(0).classArrayList has now been cleared. Where before the clear() it contained three values (1,2,3), after the call to clear the locally defined arrayList, the contents of “theList(0)” have now been cleared. Why is that? I would think since the New constructor parameter is passed by value (ByVal), locally changing a container value in the calling code would have no effect on the values previously passed to another method in a different class. What obvious principal am I missing here?

  • 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-23T18:21:27+00:00Added an answer on May 23, 2026 at 6:21 pm

    It’s passed by value, but ArrayList (like all classes) is a reference type. So you are passing the reference by value. .NET will never copy objects for you unless the object itself provides a way to do that.

    In this case you might want to use the ArrayList.Clone() method.

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

Sidebar

Related Questions

Sorry for such a basic question regarding lists, but do we have this feature
I'm sorry to ask such a basic question but I could not find a
I'm sorry for maybe making such a basic question but in ASP.NET websites what
Sorry for such a basic question, but I need to know how to add
I'm sorry to ask such a basic question, but it's kind of fundamental for
I'm very sorry for such a simple question-- I'm new at WSGI development, and
Sorry for turning to here for such a basic question, but can someone just
Sorry this is such a basic question, but I can't find a straight answer
Sorry for such a trivial question, but I'd feel silly building this if it
Sorry for the basic question - I'm a .NET developer and don't have much

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.