加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:51:38 所属栏目:百科 来源:网络整理
导读:基本上我在C#中制作一个刽子手游戏,但问题是每当我尝试用word.ToCharArray()将我的字符串单词转换为char []时.这是行不通的. 有人能弄清楚这段代码有什么问题吗? Liststring words = new Liststring();Liststring guessedLetters = new Liststring();string
基本上我在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*DesktopfinalfinalForm1.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(); 
      }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读