Solved

Passing variable in txt box..

Posted on 2014-09-22
3
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 81

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

690 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