首页 > 解决方案 > C#访问来自不同类的字典

问题描述

我一直在使用字典做一个项目,但我遇到了一个问题。在我的一个 wpf 类(表单)中,我创建了一个字典,里面有一些东西。在我的第二堂课中,我想从那本字典中阅读,所以我将字典的修饰符设置为“public”。还有问题。我的字典给出了错误:CS0050: Inconsistent accessibility: return type 'Dictionary<int, CachedSound>' is less accessible than field 'LoadAudioForm.noteValue'。你们中有人知道如何解决这个问题吗?

这是我第一堂课的一部分代码:

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"));
        }

现在是第二节课:

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

标签: c#dictionarynaudio

解决方案


看起来您正在访问它,dictionary就像它是一个static变量一样,但它不是。

如果它符合您的逻辑,您可以将其更改dictionary为静态。

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

推荐阅读