简体   繁体   中英

Error I don't understand in c# when I try to use a struct

This is the error im getting

Error 1 'System.Array' does not contain a definition for 'guess_word' and no extension method 'guess_word' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) C:\\Hangman\\Jers Hangman Game\\Jers Hangman Game\\Form1.cs 183 25 Jers Hangman Game

This is my some of my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Jers_Hangman_Game
{

public partial class Form1 : Form
{
    int rndNum = 0;
    int wordLength = 0;
    int numGuesses = 0;
    String tempWordLength = "";
    String astericks = "";
    int indexOfGuess;



    public struct Hangman_words
    {
        public int no_letters;
        public string guess_word, hint;
    }

    public Hangman_words[] myWords = new Hangman_words[19];

    public Form1()
    {
        InitializeComponent();
    }

    public void Form1_Load(object sender, EventArgs e)
    {

        myWords[0].no_letters = 5;
        myWords[0].guess_word = "earth";
        myWords[0].hint = "We live on it";

        myWords[1].no_letters = 6;
        myWords[1].guess_word = "garage";
        myWords[1].hint = "store car in it";

        myWords[2].no_letters = 11;
        myWords[2].guess_word = "grandmother";
        myWords[2].hint = "your mothers mother";

        myWords[3].no_letters = 7;
        myWords[3].guess_word = "january";
        myWords[3].hint = "The first month of the year";

        myWords[4].no_letters = 6;
        myWords[4].guess_word = "monkey";
        myWords[4].hint = "Swings from a tree";

        myWords[5].no_letters = 9;
        myWords[5].guess_word = "policeman";
        myWords[5].hint = "called a guard in ireland";

        myWords[6].no_letters = 6;
        myWords[6].guess_word = "recall";
        myWords[6].hint = "to remember";

        myWords[7].no_letters = 3;
        myWords[7].guess_word = "toy";
        myWords[7].hint = "something a child plays with";

        myWords[8].no_letters = 7;
        myWords[8].guess_word = "tobacco";
        myWords[8].hint = "smoking it gives you cancer";

        myWords[9].no_letters = 5;
        myWords[9].guess_word = "thumb";
        myWords[9].hint = "you have one on each hand";

        myWords[10].no_letters = 5;
        myWords[10].guess_word = "stiff";
        myWords[10].hint = "hard to bend";

        myWords[11].no_letters = 8;
        myWords[11].guess_word = "simplest";
        myWords[11].hint = "the most simple";

        myWords[12].no_letters = 5;
        myWords[12].guess_word = "shout";
        myWords[12].hint = "to speak very loudy";

        myWords[13].no_letters = 9;
        myWords[13].guess_word = "selection";
        myWords[13].hint = "making a choice";

        myWords[14].no_letters = 6;
        myWords[14].guess_word = "scared";
        myWords[14].hint = "To be afraid";

        myWords[15].no_letters = 6;
        myWords[15].guess_word = "palace";
        myWords[15].hint = "Where a price lives";

        myWords[16].no_letters = 5;
        myWords[16].guess_word = "adult";
        myWords[16].hint = "One who is legally allowed to vote/drink";

        myWords[17].no_letters = 11;
        myWords[17].guess_word = "explanation";
        myWords[17].hint = "a good answer";

        myWords[18].no_letters = 4;
        myWords[18].guess_word = "calm";
        myWords[18].hint = "To be at ease";

        myWords[19].no_letters = 4;
        myWords[19].guess_word = "kids";
        myWords[19].hint = "children";


    }

    private void button1_Click(object sender, EventArgs e)
    {
        wordHint.Text = myWords[randomNumGenerator()].hint;
        tempWordLength = myWords[randomNumGenerator()].guess_word;
        wordLength = tempWordLength.Length;
        astericksBox.Text = asterickGenerator(tempWordLength);
        astericks = "";
        guesses.Text = numGuesses.ToString();
    }



    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void wordHint_TextChanged(object sender, EventArgs e)
    {

    }

    private void ResetWord_Click(object sender, EventArgs e)
    {
        wordHint.Clear();
        astericksBox.Clear();
        wordHint.Text = myWords[randomNumGenerator()].hint;
        tempWordLength = myWords[randomNumGenerator()].guess_word;
        wordLength = tempWordLength.Length;
        astericksBox.Text = asterickGenerator(tempWordLength);
        astericks = "";
    }

    private int randomNumGenerator() {
        // random number generator
        Random randomNum = new Random();
        rndNum = randomNum.Next(0,19);
        return rndNum;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {


    }

    private String asterickGenerator(String guess) {
        for (int i = 0; i < wordLength; i++) {
            astericks += "*";
        }
        return astericks;
    }

    private void charGuess( char letter ) {
        if (myWords.guess_word.Contains(letter)) {
        numGuesses+=1   

        }

    }

The error is in the last method charGuess.

You're not using an indexer in your last method, so you're trying to (as the message says) call the method on the array itself, rather than a member of the array.

myWords[0].guess_word is accessing the guess_word field on the 0th element in the array. myWords.guess_word is attempting to access the non-existent guess_word field on the array itself.

Edit: in addition to the error @Peyman pointed out, and expanding on @pm100 comment, you need to select the word you're guessing and store it as a field. In your method button1_Click you're selecting a word from your array, but not storing it. Also, because you call randomNumGenerator() twice, your hint and word length are very likely to be from different elements in the array.

Further edit: if I may... Create a method to select a word and call it instead of duplicating similar work in 2 event handlers.

private void SelectNewWord()
{
    wordHint.Clear();
    astericksBox.Clear();
    // word = new field of type Hangman_words in class.
    // random = new static field of type Random in class.
    word = myWords[random.Next(0, myWords.Length)];
    wordHint.Text = word.hint;
    astericksBox.Text = new string('*', word.quess_word.Length);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM