4

Basically I'm making a hangman game in C# but the problem is whenever I try to turn my string word to char[] with word.ToCharArray(); it does not work.

Can someone figure out what is wrong with this code?

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

2
  • Looks like you're doing this outside of a method body against instance fields. If this is what you mean to do, put the initialization of letters in the constructor, or define a proper method for your code. Commented Jun 1, 2018 at 9:06
  • Please post your whole code that you are using. Commented Jun 1, 2018 at 9:21

5 Answers 5

1

So far what is known, you have a class named 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(); 
      }
}

If this is the case then, you are doing it wrong. You will need an object of class Form1 to use variable word.

      public static void Main(string[]args){
          Form1 F1 = new Form1();
          char[] letters = F1.word.ToCharArray(); 
      }
Sign up to request clarification or add additional context in comments.

Comments

0

error says a static field is required under a static method for a call, so change word as static -

static string word = "sword";

Comments

0

Technically, you can move the initialization into the constructor:

  public partial class Form1: Form {
    ...
    List<string> words = new List<string>();
    List<string> guessedLetters = new List<string>();
    string word;

    // Turning word into char array
    char[] letters;

    public Form1() {
      word = "sword"; 
      letters = word.ToCharArray();         
    }  
  }

Another possibility is to declare property: whenever you want to have word split into letters just call letters property

  public partial class Form1: Form {
    ...
    List<string> words = new List<string>();
    List<string> guessedLetters = new List<string>();
    string word = "sword";

    ...

    private char[] letters {
      get {
        return null == word 
          ? new char[0]
          : word.ToCharArray(); 
      }
    } 

Comments

0

I suspect you have something like this that you can fix with a static.
But this is probably not the best design.

class Program
{
    List<string> words = new List<string>();
    List<string> guessedLetters = new List<string>();
    static string word = "sword";
    char[] letters = word.ToCharArray();

This might be a better design:

public List<string> words { get; } = new List<string>();
public List<string> guessedLetters { get; } = new List<string>();
public string word { get; set; } = "sword";
public char[] letters { get { return word.ToCharArray(); } }

Comments

0

From Microsoft Docs: "Instance fields cannot be used to initialize other instance fields outside a method."

Its a compiler decision and that's just the way it is. Or setting a constant with a variable, little things like that are design decisions and there are seasons behind them big or small like a matter of taste.

You can fix it by making word a static but I don't know if that is what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.