I’m creating a simple mp3 player just to see how Dictionary works. I have 6 classes in my project which 2 of them are irrelevant. The first class includes tags, the second is search and the third is play, and the fourth which is an abstract class, includes the dictionary. Both search and play classes inherit from the fourth class.
When I go to search, objects are successfully added to the dictionary Dictionary dict<string, Tags>, and tags are added from Tags class.
My problem is, when I try to reach from Play class to get the path from dictionary it says the key is not present.
The code is dict[playSongName].Path;, where playSongName is the name of the song with which it is stored in the dictionary.
class Player : Liste
{
public void Play(string playSongName)
{
currentSongPath = dictPesmi[playSongName].Path; // Error here, key was not found
currentSongName = playSongName;
media.Source = new Uri(currentSongPath);
media.Play();
}
}
abstract class Liste
{
public Dictionary<string, Tagi> dictPesmi = new Dictionary<string, Tagi>();
public Id3TagClass id3tagi = new Id3TagClass();
}
class Search : Liste
{
string[] filesFound;
const string extension = "*.mp3";
public void Find(string searchPath)
{
if (Directory.Exists(searchPath))
{
filesFound = Directory.GetFiles(searchPath, extension, SearchOption.AllDirectories);
foreach (string filePath in filesFound)
{
string[] filePathSplit = filePath.Split('\\');
string fileName = filePathSplit[filePathSplit.Length - 1];
dictPesmi[fileName] = new Tagi() { Path = filePath, Name = fileName };
dictPesmi[fileName].Author = id3tagi.GetArtist(filePath);
dictPesmi[fileName].Album = id3tagi.GetAlbum(filePath);
dictPesmi[fileName].Title = id3tagi.GetTitle(filePath);
System.Windows.MessageBox.Show(dictPesmi[fileName].Path);
}
}
}
}
class Tagi
{
string path;
string name;
string title;
string author;
string album;
public string Album
{
get { return album; }
set { album = value; }
}
public string Author
{
get { return author; }
set { author = value; }
}
public string Title
{
get { return title; }
set { title = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Path
{
get { return path; }
set { path = value; }
}
}
Main class
public partial class MainWindow : Window
{
Player player = new Player();
Search search = new Search();
// string selectedSong;
public MainWindow()
{
InitializeComponent();
}
public void KeyDownEvent_(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1)
player.Play(lbPlaylist.Items[lbPlaylist.Items.IndexOf(lbPlaylist.SelectedItem)].ToString());// passed string
if (e.Key == Key.F3)
search.Find(tbSearch.Text);
if (e.Key == Key.F5)
{
foreach (Tagi d in search.dictPesmi.Values)
{
if (!lbPlaylist.Items.Contains(d.Name))
lbPlaylist.Items.Add(d.Name);
}
}
if (e.Key == Key.F9)
{
}
}
It’s when i press the F1 the described error shows up
Your problem here is the fact that you are filling the Dictionary contained in the instance of Search, but you are looking for the data in the one contained in the instance of Player, which is still empty.
Everytime you create an instance of a class derived from Liste a new empty Dictionary is created. Things would work modifying your code as follows, but this is not really object oriented, I consider it really “ugly” and I would change the design of your application instead: