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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:33:13+00:00 2026-05-30T05:33:13+00:00

I have a designed a Class which acts as if it is a CD,

  • 0

I have a designed a Class which acts as if it is a CD, I have also another Class which tests the Class I have designed.
The test class makes a new object of CD and fills it with variables etc..

My problem is that the testing Class says my Class fails due to a variable array changing after the object has been made and these variables aren’t directly changing the actual Class properties, how do I stop this?

Test Class:

string[] myCdTracks = { "Song 1", "Song 2", "Song 3" };
string[] myCdTracks2 = { "Song 1", "Song 2", "Song 3" };

CD myCd = new CD("My Songs", "Fergus", "Rock", myCdTracks);
for (int i = 0; i < myCdTracks.Length; i++)    
    {

It changes its value here when it shouldn’t be.

        myCdTracks[i] = "aaaa";
    }
    result = "PASS";
    if (myCd.Tracks.Length != testArray.Length)
    {
        result = "FAIL";
    }
    else
    {
        for (int i = 0; i < myCd.Tracks.Length; i++)
        {
            if (myCd.Tracks[i] != testArray[i])
            {
                result = "FAIL";
            }
        }
    }

My CD Class:

public class CD
{
    string _Name;
    string _Artist;
    string _Genre;
    string[] _Tracks;

    public CD(string name, string artist, string genre, string[] tracks)
    {
        _Name = name;
        _Artist = artist;
        _Genre = genre;
        _Tracks = tracks;
    }

    // ... Some properties

    public string[] Tracks
    {
        get
        {
            return _Tracks;
        }
        set
        {
            for (int i = 0; i < _Tracks.Length; i++)
            {
                _Tracks[i]= value[i];
            }
        }
    }
}
  • 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-30T05:33:14+00:00Added an answer on May 30, 2026 at 5:33 am

    My problem is that the testing Class says my Class fails due to a variable array changing after the object has been made and these variables aren’t directly changing the actual Class properties, how do I stop this?

    The problem is that you’ve just copied the reference to the array of track names in the constructor. That means any changes to the array from the outside will still be visible via your reference. One simple option is to clone the array:

    _Tracks = (String[]) tracks.Clone();
    

    … or even change the variable type and create a read-only collection:

    _Tracks = new ReadOnlyCollection<string>(tracks);
    

    Fortunately you don’t need to worry about the contents of the string objects themselves changing, as strings are immutable. It is important that you understand what’s going on with your program in its current form though – and my article on reference types vs value types may help.

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

Sidebar

Related Questions

I have designed a class containing some information about a given object which will
I have designed a class which is basically nothing but an object which stores
I have designed an ASP.NET page which create graphs. I have written a class
I have a class which implements IHttpHandler that is designed to handle image resize
I am working on a class library using C#. I have designed 3 main
I have a class (class A) that is designed to be inherited by other
I have some C# class libraries, that were designed without taking into account things
I have designed this gui in netBeans which has a canvas , a couple
I have designed a new web site. I have hosted it online. I want
I have designed a Class for Parent Child relationship class Category { public string

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.