Chris Blackmon
Chris Blackmon

Reputation: 197

Checking a Property of an Object In a Queue

I need to compare a name of an object (test) with the name of a test that has been placed into a queue. The logic I have is to use a foreach loop so that for each test in the queue I can compare the name that the user provides with the name on each test until it finds a match (in which it will tell the user the score they made on the test in a message box).

The code in the snippet is incomplete; using submittedTests with a getter doesn't work (doesn't give me an option to do so in intellisense).

This takes place in the btnFindTest_Click method. This is the code that I have so far:

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

namespace ConsoleApplication1
{
    public partial class Form1 : Form
    {
        //Stack and Queue calls
        Queue submittedTest = new Queue();
        Stack outForChecking = new Stack();

        public Form1()
        {
            InitializeComponent();
        }



        private void btnSubmitTest_Click(object sender, EventArgs e)
        {
            //generates a random test score
            Random rdm = new Random();
            int testScore = rdm.Next(0, 100);
            string score = testScore.ToString();

            //assigns user input to a variable
            string name = txtName.Text;

            //Generate a new test that passes in 
            Test tests = new Test(name, score);

            //shows the user the name they just enetered
            label3.Text = String.Format("{0}", name);

            //adds submitted test to the queue, then displays that test in a list box
            submittedTest.Enqueue(tests);
            listSubTests.Items.Add(new Test(name, score));

            //Clears input box for next user input
            txtName.Clear();
        }

        private void btnFindTest_Click(object sender, EventArgs e)
        {
            string compareName = "";

            string tempName = txtName.Text;

            foreach (Test tests in submittedTest)
            {
                if (compareName == tempName)
                {
                    System.Windows.Forms.MessageBox.Show("Your score was --");
                }
            }

        }



        public void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtScore_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}

And the test object is defined in it's own class here

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Test
    {

        private string name;
        private string score;

        public string Name 
        {
            get { return name; }
            set { name = value; }
        }
        public string Score 
        {
            get { return score; }
            set { score = value; }
        }

        public Test(string name, string score)
        {
            this.name = name;
            this.score = score;
        }

        public override string ToString()
        {
            return (String.Format("{0} {1} ", name, score));
        } 
    }
}

I'm reletively new to C# and this project is for school, so if I'm far off, please let me know!

Upvotes: 4

Views: 359

Answers (1)

ESG
ESG

Reputation: 9425

Based on your example, you may have forgotten to use the object:

foreach (Test tests in submittedTest)
{
    if (tests.Name == tempName)
    {
        System.Windows.Forms.MessageBox.Show("Your score was --");
    }
}

Upvotes: 2

Related Questions