// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; namespace E { public class MP3Info { private string artist; private string album; private string title; public MP3Info() : this("", "", "") { } public MP3Info(string artist, string album, string title) { this.artist = artist; this.album = album; this.title = title; } public string Artist { get { return artist; } set { artist = value; } } public string Album { get { return album; } set { album = value; } } public string Title { get { return title; } set { title = value; } } public override string ToString() { return ("[" + artist + "," + album + "," + title + "]"); } } public class MP3InfoCollection { private Dictionary> artists = new Dictionary>(); private Dictionary> albums = new Dictionary>(); private Dictionary> titles = new Dictionary>(); public void Add(MP3Info mp3) { if(!artists.ContainsKey(mp3.Artist)) { artists[mp3.Artist] = new List(); } artists[mp3.Artist].Add(mp3); if(!albums.ContainsKey(mp3.Album)) { albums[mp3.Album] = new List(); } albums[mp3.Album].Add(mp3); if(!artists.ContainsKey(mp3.Title)) { titles[mp3.Title] = new List(); } titles[mp3.Title].Add(mp3); } public List FindByArtist(string artist) { if(artists.ContainsKey(artist)) { return artists[artist]; } else { return null; } } public List FindByAlbum(string album) { if(albums.ContainsKey(album)) { return albums[album]; } else { return null; } } public List FindByTitle(string title) { if(titles.ContainsKey(title)) { return titles[title]; } else { return null; } } } public class MainClass { public static void Main(string[] args) { MP3InfoCollection col = new MP3InfoCollection(); col.Add(new MP3Info("Band 1", "Album 1", "Nummer 1")); col.Add(new MP3Info("Band 1", "Album 1", "Nummer 2")); col.Add(new MP3Info("Band 1", "Album 2", "Nummer 1")); col.Add(new MP3Info("Band 1", "Album 2", "Nummer 2")); col.Add(new MP3Info("Band 2", "Album 1", "Nummer 1")); col.Add(new MP3Info("Band 2", "Album 1", "Nummer 2")); foreach(MP3Info mp3 in col.FindByArtist("Band 1")) { Console.WriteLine(mp3); } foreach(MP3Info mp3 in col.FindByAlbum("Album 1")) { Console.WriteLine(mp3); } Console.WriteLine(col.FindByArtist("XXX")==null); Console.ReadLine(); } } }