1

I have been trying to get this class to work. When I remove static from all of the methods the class works but I am unable to call the display() from a different class. In order to call the display() I have to make it static along with everything else in the class. Now I am getting an error "Static field or property 'trackName' cannot be assigned in an object initializer' for this line of code: tracklist.Add(new Tracklist() {trackName=track, minutes=Minutes, seconds=Seconds}); I am also getting the error with 'minutes' and 'seconds'.

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.IEnumerable;

namespace Music
{
class Tracklist
{
    public static string trackName { get; set; }
    public static int minutes { get; set; }
    public static int seconds { get; set; }
    //public string strng { get; set; }
    public static List<Tracklist> tracklist = new List<Tracklist>();
    //public static List<string> TrackListString = new List<string>();
    //public static string[] popper;

    public Tracklist()
    {
        //public List<Tracklist> tracklist = new List<Tracklist>();
        display();
    }
    public static void add(string track, int Minutes, int Seconds)
    {
        //string strng = Track.Track(track,Minutes,Seconds);
        tracklist.Add(new Tracklist() {trackName=track, minutes=Minutes, seconds=Seconds});
        //tracklist.Add(trackName=track, minutes=Minutes, seconds=Seconds);
        //new Tracklist() { strng };
    }
    public static int count()
    {
        return tracklist.Count;
    }
    public static string ToString()
    {
        int i=0;
        string[] holder=new string[count()];
        string c = "";
        foreach(object strng in tracklist)
        {
            string a = Track.Track(trackName, minutes, seconds);
            string b = (Convert.ToString(i+1) + ") " + a + Environment.NewLine);
            c = c + b;
            //TrackListString.Add(new string() { strng = b });
            holder[i] = b;
            i++;
            //return b;
        }
        return c; 
    }
    public static string getTrackAt(int i)
    {
        //string TrackAtI= tracklist.[i].trackName; // not sure what is heppening here
        string test = tracklist.ElementAt(i).trackName;
        return test;
    }
    public static void display()
    {
        string holder = ToString();
        Console.Write(holder);
    }       
}}

Why am I getting this error?

2
  • Why do you use static properties and methods?! I think using of static key word isn't necessary for your project!! Commented Mar 9, 2014 at 4:50
  • I made all of my properties non-static and got the code to work. Commented Mar 18, 2014 at 2:56

3 Answers 3

2

display() should not be static, because when you display a track list, you intend to display an instance of a track list. Making it static would mean that you display the notion of track lists in general, not a specific track list.

This is also the reason why you're getting this error. You're creating an instance of track list by calling new Tracklist(), but trackName is not a property of a track list. It is static, making it a property of the notion of track lists in general. There's no trackName per track list, there's only one track name in the world!

If trackName is kept static, it means that tracklist1.trackName is llegal. There's only Tracklist.trackName, so there's only one track name in the world.

Sign up to request clarification or add additional context in comments.

2 Comments

In my other class I call the display in the Tracklist class. By making the display not static I get an error in the other class that reads "An object reference for the non-static field, method, or property 'Music.Tracklist.display()'.
@Maclover25 That's probably because you're calling display() on the class: Tracklist.display(), while you should call it on an instance: var tl = new Tracklist(); tl.display();
0

You have this sub-expression:

new Tracklist() {trackName=track, minutes=Minutes, seconds=Seconds}

but the properties trackName, minutes, seconds are static properties. So they don't belong to the new Tracklist instance you have created. The error message is clear:

Static field or property cannot be assigned in an object initializer.

An "object initializer" is an expression like (example):

new NameOfType() { Property1 = expr1, Property2 = expr2, }

It makes a new instance (a new object) of the type, and sets each of the properties on that type. Therefore the properties must certainly be instance properties, a.k.a. non-static properties.

Comments

-1

I agree with the other answer that display() should not be static. But if that's the case then the rest of your class should not be static either. I'm assuming that you're trying to implement a singleton here. Firstly, as also mentioned above, trackName, minutes and seconds can't be static. But you can add a static constructor to initialize your single instance:

static class TrackList
{
    static TrackList()
    {
        tracklist = new List<TrackList>();
    }
}

The non-static constructor is the source of the error.

1 Comment

Singletons are a bad practice except for very extraordinary circumstances, especially the kind that are instantiated in the static constuctor (WTF?) and this is a piece of spectacularly bad advice from the point of view of object-oriented design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.