C#错误:上下文关键字'var'可能只出现在局部变量声明中

Yas*_*iya 0 c# var compiler-errors winforms

我编写了以下函数来创建axWindowsMediaPlayer播放列表:

WMPLib.IWMPPlaylist p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1");

private void CreatePlaylist(string _currentId)
{
  string selectedElementPageTypeValue = MainContentAreaBl.GetSelectedElementPageTypeValue(_currentId);
  var selectedElementJumpToValue = MainContentAreaBl.GetSelectedElementValue(_currentId, "jumpTo");
  if (selectedElementJumpToValue != null)
  {
     _currentId = selectedElementJumpToValue;                
     if (_currentId != null && _currentId != "menu" && selectedElementPageTypeValue == "video")
     {
        var playerFile = Path.Combine(Common.ContentFolderPath, MainContentAreaBl.GetSelectedElementDataPathValue(_currentId));
        p2.appendItem(axWindowsMediaPlayer.newMedia(playerFile));
        axWindowsMediaPlayer.currentPlaylist = p2;
        CreatePlaylist(_currentId);
     }                
     axWindowsMediaPlayer.Ctlcontrols.play();
  }
}
Run Code Online (Sandbox Code Playgroud)

var p2是在班级宣布的.当我编译我的应用程序时,我收到以下错误消息:

上下文关键字'var'可能只出现在局部变量声明中

但是,我不能放入var p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1");递归函数,因为它会在每次迭代时创建新的播放列表.

如何在我的函数中访问p2?

编辑1:我在输出窗口中看到了这一点

COM参考'WMPLib'是ActiveX控件'AxWMPLib'的互操作程序集,但标记为由编译器与/ link标志链接.此COM引用将被视为参考,不会链接.

此外,现在它显示以下错误axWindowsMediaplayer:

字段初始值设定项不能引用非静态字段,方法或属性

这些信息是否与我所看到的错误有关?怎么去解决这个问题?

ang*_*son 10

您必须使用正确的类型声明它,而不是使用var:

AxWMPLib.IWMPPlaylist p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1");
Run Code Online (Sandbox Code Playgroud)

var仅允许在局部变量上,而不是在字段上,这就是错误消息告诉您的内容.错误消息并不意味着该字段在错误的位置声明,您只是使用了错误的字段类型语法.