Hey guys can anyone tell me what am I doing wrong?

I basically set up a method which has an array, it will return a specific element in the array according to an ID set within my property.

I get the following errors:

Error (wordIdFunc = 2;): Invalid token '=' in class, struct, or interface member declaration	

Error (return words[wordId];): The name 'wordId' does not exist in the current context

Heres the code:

public partial class TypingTutor : Form
    {
        private int wordIdFunc;

        public int propWordId
        {
            get
            {
                return wordIdFunc;
            }

            set
            {
                wordIdFunc = value;
            }
        }

        wordIdFunc = 2;

        public TypingTutor()
        {
            InitializeComponent();
        }

        public string Word ()
        {
            string[] words;
            words = new string[4];

            words[0] = "The";
            words[1] = "dog";
            words[2] = "jumps";
            words[3] = "sideways.";

            int wordCount = words.Length;

            return words[wordId];
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Word: " + Word());
        }
    }

Thank you. :)

Recommended Answers

All 3 Replies

wordId on line 37 is not defined.
and where is wordIdFunc = 2; ?

This should do it:

public partial class Form1 : Form
    {
        private int wordIdFunc;
        public int propWordId
        {
            get { return wordIdFunc; }
            set { wordIdFunc = value; }
        }

        public Form1()
        {
            InitializeComponent();
        }       

        public string Word()
        {
            string[] words = new string[4];
            words[0] = "The";
            words[1] = "dog";
            words[2] = "jumps";
            words[3] = "sideways.";

            //get the variable:
            return words[wordIdFunc];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //set the variable:
            propWordId = 3;

            //show the message:
            MessageBox.Show("Word: " + Word());
        }
    }

Mitja

Ha Mitja! you are just ahead of me I was going to post EXACTLY the same.
Rest me to say that wordIdFunc is a private variable and as such not exposed by a class except here where the property propWordId is defined in the same class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.