samedi 17 mars 2018

Why does my data[i] always has the same value for its property "indexTarget"?

I have been working on this code and I'm not able to understand whats wrong with it. This is a brief of what the program does:

  • WordsData class; randomly generate a 'indexTarget', search for it in a txt file and return the correspondent word for the 'indexTarget'.

  • WordGenerator class; will show the word in the screen, check if is correct, and move from left to right.

The txt file it's in this format index:word:

The problem it's happening in this for loop,

for (i = 0; i <= data.Length - 1; i++)
        {
            data[i] = new WordsData();
        }

What is happening is that if I run the program with a breakpoint in this line data[i] = new WordsData(); and go step by step data[i].indexTarget will have a diferent value for each instance.

But if I simply run the program the data[i].indexTarget will have the same value.

This is the all the code I have:

 public partial class FrmWordsApp : Form
{
    WordGenerator[] Word = new WordGenerator[10];
    WordsData[] data = new WordsData[10];
    int wordCounter = 0;

    public FrmWordsApp()
    {
        Random rnd = new Random();
        int i = 0;

        InitializeComponent();

        for (i = 0; i <= data.Length - 1; i++)
        {
            data[i] = new WordsData();
        }

        for (i = 0; i <= Word.Length - 1; i++)
        {                
            Word[i] = new WordGenerator(0, rnd.Next(1, 11) * 50, data[i].GetWord());
        }
    }
}

and this is the WordsData class:

class WordsData
{
    private int indexTarget;
    Random target = new Random();

    public WordsData()
    {
        indexTarget = target.Next(1, 20);
    }


    public string GetWord()
    {

        string Word = null;
        string index = null;
        Boolean indexFound = false;
        string path = @"c:\Users\User\source\repos\WordsApp\WordsApp\words_file_final.txt";


        // Go through the file and find the word corresponding to indexTarget 
        // and return that word in a string.
        try
        {
            using (StreamReader SR = new StreamReader(path))
            {
                while (indexFound == false)
                {
                    int i = 0;
                    index = null;
                    string content = SR.ReadLine();
                    while (content[i] != ':')
                    {
                        index = index + content[i].ToString();
                        i++;
                    }


                    if (Convert.ToInt32(index) == indexTarget)
                    {
                        // parse the line to get the word target and set the loop to false.
                        for (i = index.Length + 1; i <= content.Length - 2; i++)
                        {
                            Word = Word + content[i].ToString();
                        }
                        indexFound = true;
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return Word;
    }


}

and this is the WordGenerator class

public class WordGenerator
{
    Label word = new Label();
    int x;
    int y;
    //Boolean isCorrect;


    public WordGenerator(int initial_X, int initial_Y, string txt)
    {
        x = initial_X;
        y = initial_Y;
        word.Text = txt;
        //isCorrect = false;
    }

    public void Show(Panel pnlDisplay)
    {
        word.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, FontStyle.Bold);
        word.Location = new System.Drawing.Point(x, y);
        word.Parent = pnlDisplay;
    }

    public void Move()
    {
        x = x + 10;
        word.Location = new System.Drawing.Point(x, y);
        if (x > 800)
        {
            word.ForeColor = Color.Yellow;
        }
        if (x > 950)
        {
            word.ForeColor = Color.Red;
        }
    }

    public void CheckWord(TextBox userWord)
    {
        if (userWord.Text == word.Text)
        {
            //word.ForeColor = Color.Green;
            //isCorrect = true;
            word.Text = "";
        }
    }
}

I apriciate all help, Thanks




Aucun commentaire:

Enregistrer un commentaire