微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

字符串到char数组c#导致错误

基本上我在C#中制作一个刽子手游戏,但问题是每当我尝试用word.tochararray()将我的字符串单词转换为char []时.这是行不通的.

有人能弄清楚这段代码有什么问题吗?

List<string> words = new List<string>();
List<string> guessedLetters = new List<string>();
string word = "sword";

// Turning word into char array
char[] letters = word.tochararray();

CS0236 A field initializer cannot reference the non-static field,method,or property ‘Form1.word’ final C:\Users*\Desktop\final\final\Form1.cs 20 Active

解决方法

到目前为止已知的是,你有一个名为Form1的类:

public class Form1 {
      List<string> words = new List<string>();
      List<string> guessedLetters = new List<string>();
      string word = "sword";
      //...
      public static void Main(string[]args){
          char[] letters = word.tochararray(); 
      }
}

如果是这种情况那么,你做错了.您将需要Form1类的对象来使用变量word.

public static void Main(string[]args){
          Form1 F1 = new Form1();
          char[] letters = F1.word.tochararray(); 
      }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐