2

I have this short windows form application code that i cannot get to work properly. It uses an instance of a class (as a temporary storage place) to populate an array of the same class type. It has a button and a textbox. If you press the button, the text is saved in the Name member and added to the array. If the Name is taken by another array member, it should show a message "name taken!" and not add it to the array. The problem is that this condition check is always true! if we type in "Marry", the debugger shows that

 if (examleArray[i].Name == temp.Name)

is equivalent to:

if ("Marry" == "Marry")

as if one is pointing to the other. Why is this? how do i fix it? thank you!

namespace errorexample
{
    public partial class Form1 : Form
    {

        example temp = new example();
        example[] examleArray = new example[10];
        int examleArrayIndex = 0;
        bool NameTaken = false;

        public Form1()
        {  InitializeComponent();  }

        private void button1_Click(object sender, EventArgs e)
        {
            temp.Name = textBox1.Text;         
            NameTaken = false;

            for (var i = 0; i < (examleArrayIndex); i++)
            {
                if (examleArray[i].Name == temp.Name)
                {
                    NameTaken = true;
                    MessageBox.Show("Name taken!");
                }

            }

            if (NameTaken == false)
            {
                examleArray[examleArrayIndex] = temp;
                examleArrayIndex++;
            }
        }
    }

    public class example {
        public string Name;  
    }

}
2
  • 7
    Because there is only one instance of example in your form, you keep reusing it Commented Dec 2, 2016 at 17:42
  • 1
    Because you have an array of references that all point to the same example object since you only create one. Commented Dec 2, 2016 at 17:44

3 Answers 3

1

You only have one temp object, and you add keep adding it to the array. It's the same temp since you never create a new one. I've rewritten it for you:

 List<example> examleArray = new List<example>();
 private void button1_Click(object sender, EventArgs e)
 {
   if (examleArray.Any(e=>e.Name == textBox1.Text))
   {
     MessageBox.Show("Name taken!");
   } else {
     examleArray.Add(new example { Name = textBox1.Text });
   }
 }

I've also converted your fixed array into a List so you don't ever accidentally try and add 11 names and then blow up. And I've converted your search to LINQ to simplify.

Sign up to request clarification or add additional context in comments.

Comments

0

You only create one example object. You are putting multiple references to the same object into the array. One solution is to create a new example object to put in the array:

public class example {
    public string Name;
    public example( string name ){
        Name = name;
    }
}

//...
examleArray[examleArrayIndex] = new example( temp.Name );

Comments

0

move temp object initialization inside the click event, otherwise you are updating same object

private void button1_Click(object sender, EventArgs e)
        {
            example temp = new example();
            temp.Name = textBox1.Text;         
            NameTaken = false;

            for (var i = 0; i < (examleArrayIndex); i++)
            {
                if (examleArray[i].Name == temp.Name)
                {
                    NameTaken = true;
                    MessageBox.Show("Name taken!");
                }

            }

            if (NameTaken == false)
            {
                examleArray[examleArrayIndex] = temp;
                examleArrayIndex++;
            }
        }

you can use List

List<string> example =
        new List<string>();

if(example.Contains(textBox1.Text))
{ 
  MessageBox.Show("Name taken!");
}else
{
  example.Add(textBox1.Text);
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.