Solved

Passing variable in txt box..

Posted on 2014-09-22
3
161 Views
Last Modified: 2014-09-30
Not sure what is wrong with this code from my .aspx page... Not getting any result when I click on the button.

 protected void Button1_Click(object sender, EventArgs e)
    {
        List<Person> People = new List<Person>();


        // private List<Person> people = new List<Person>();

        Person person = new Person();
        {
            person.FirstName = TextBox1.Text;
            person.LastName = TextBox2.Text;            

        };
        if (People.Contains(person))
        {

            Label1.Text = "The list does not contains this person";

        }
        else {
            People.Add(person);
            TextBox1.Text = "";
            TextBox2.Text = "";
        
        }

Open in new window




public class Person : IEquatable<Person>
{
	
		public string LastName { get; set;}
        public string FirstName { get; set;}


       public bool Equals(Person other){
       
        return ((FirstName == other.FirstName) && (LastName == other.LastName));
       
       
       }

}

Open in new window

0
Comment
Question by:yguyon28
  • 2
3 Comments
 
LVL 79

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 40337465
Other than it always says that the user exists even if it doesn't change your code to:
               if (!(People.Contains(person)))
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40338927
Your list is local to the click handler.  You are recreating the list every time you click the button, so it is always empty. You need to widen the scope of the list variable--i.e. make it a class-level variable instead of a method-level variable.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40353138
Not sure why the selected answer has been chosen. That condition will always be true based on what I mentioned above.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question