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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:12:12+00:00 2026-05-26T22:12:12+00:00

I have a program where I have three classes A, B and C. A

  • 0

I have a program where I have three classes A, B and C.
A contains a member that is a list of B, and B consists of a member that is a list of C.
A contains a method that is called RemoveB(), and similarly B contains a method that is called RemoveC(). RemoveB() and RemoveC() remove the first element of the BList and the CList respectively.

The following is what it all looks like:

class A
{
    ...
    List<B> BList;
    ...
    public void RemoveB()
    {
        this.BList.Removeat(0)
    }
}

class B
{
    ...
    List<C> CList;
    ...
    public void RemoveC()
    {
        this.CList.Removeat(0)
    }
}

class Trial
{
    ...
}

In my main function I first populate BList and the CList in each of these BList elements. There are N elements in BList and each of the BList elements contains M elements in CList. Then I do the following.

A aObject = new A(); // Populates BList and CList.

for(int i = 0; i < N; i++)
{
    Console.WriteLine("BList: {0} ({1})", i, aObject.BList[0].Count);

    for (int j = 0; j < M; j++)
    {
        Console.WriteLine("CList: {0}", j);
        aObject.BList[0].RemoveC();
    }
    aObject.RemoveB();
}

When I do this, I get the following problem. When I try to remove elements in CList belonging to element BList[0], the corresponding elements in CList in BList[1], BList[2]… BList[N-1] are also removed.
The following is the output I see from the running the program.

BList 0 (M)
CList 0
CList 1
CList 2
.
.
.
CList M-1
BList 1 (0)
BList 2 (0)
BList 3 (0)
.
.
.
BList N-1 (0)

Could someone tell me why this could be happening? Am I doing some fundamental mistake somewhere.

  • 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-26T22:12:12+00:00Added an answer on May 26, 2026 at 10:12 pm

    Looks like a problem with how you’re creating the B and C elements in the A constructor. Are you using the new keyword to create each individual B element and C element and list of C elements? If you’re just copying them at some point, then they may still be the same actual object in memory, and changing or deleting one of them could do the same thing to the others as well.

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

Sidebar

Related Questions

THIS IS HOMEWORK: I have a program that consists of two winforms and three
Lets say we have a program which contains such classes: public interface AbstractItem {
I have two classes: Action class, that has a method for executing VBScript files,
I have a test program called ftest. It loads .so files that contain tests
I'm deserializing a class called Method using .NET Serialization. Method contains a list of
I have a base class that three classes inherit from. Whenever these child classes
i have a css in my GWTApplication which contains several classes in that,I want
I have three different development machines. Do I need three different iPhone Developer Program
I have a program, and in that program there is some variables (username and
I have a program that creates a JFrame and makes it visible. Is there

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.