Alexander Bunt
Alexander Bunt

Reputation: 23

C# Access dictionary from different class

I've been working on a project using a dictionary, but I got a problem. In my one wpf class (form) I create a dictionary with some stuff inside. In my second class, I want to read from that dictionary, so I've set the modifier of my dictionary to 'public'. And there is the problem. My dictionary gives the error: CS0050: Inconsistent accessibility: return type 'Dictionary<int, CachedSound>' is less accessible than field 'LoadAudioForm.noteValue'. Does any of you know how to fix this?

This is a part of my code of my first class:

public partial class LoadAudioForm : Form
{
    public Dictionary<int, CachedSound> noteValue = new Dictionary<int, CachedSound>();

private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        var worker = sender as BackgroundWorker;
        for (int i = 36; i < 97; i++)
        {
            noteValue.Add(i, new CachedSound("E:/VirtualCarillon/VirtualCarillon/VirtualCarillon/VirtualCarillon/Audio/01/" + i + ".wav"));
        }

And now the second class:

AudioPlaybackEngine.Instance.PlaySound(LoadAudioForm.noteValue[ne.NoteNumber + (Convert.ToInt32(nVelR) * 100)]);

Upvotes: 1

Views: 752

Answers (2)

Bizhan
Bizhan

Reputation: 17085

It's as the error says: The type of the field is less accessible than the field itself.

The field is public so the field's type must be at least public or otherwise the compiler will complain about this inconsistency.

The field's type in your code is Dictionary<int, CachedSound>; We know Dictionary and int are public, so check CachedSound's access modifier and make sure it's not internal or private.

Upvotes: 0

Misha Zaslavsky
Misha Zaslavsky

Reputation: 9646

It looks like you are accessing the dictionary like it is a static variable but it is not.

If it fits your logic, you can change the dictionary to be static.

public static Dictionary<int, CachedSound> noteValue =
    new Dictionary<int, CachedSound>();

Upvotes: 1

Related Questions