user661025
user661025

Reputation:

Class property changing

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];
            }
        }
    }
}

Upvotes: 0

Views: 127

Answers (2)

MartinStettner
MartinStettner

Reputation: 29164

Also note, that in your property setter for the Tracks property, you'll most likely run into troubles if the array to be set has more elements than the current _Tracks array.

I suggest to use Jon Skeet's variant in both the constructor and the property setter.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500105

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.

Upvotes: 3

Related Questions