c# - 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)]);
解决方案
看起来您正在访问它,dictionary
就像它是一个static
变量一样,但它不是。
如果它符合您的逻辑,您可以将其更改dictionary
为静态。
public static Dictionary<int, CachedSound> noteValue =
new Dictionary<int, CachedSound>();
推荐阅读
- python - 检查类的子/父关系,同时防止循环导入/循环依赖
- android - 如何下载 android 支持存储库和 google 存储库
- javascript - Jest 在测试 Vue 单文件组件时遇到了意外的令牌
- android - 材料卡片视图和卡片视图有什么区别?为什么有些属性不适用?
- ionic-framework - Ionic 4 导航到包含新数据的页面
- assembly - 汇编(英特尔):“调用地址”与内存中的结果字节有何关系?
- dynamics-crm - 如何传递 EntityReference 以在 Microsoft Dynamics 365 CRM 中的查找字段上添加属性值
- flutter - 如何为`showSearch`提供一个BLoC(带有flutter_bloc)
- ios - Xamarin iOS - 广播上传扩展 - 如何调用
- windows - 使用 Powershell 将文件名和文件内容的值分配给模板