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?