Solved

Passing variable in txt box..

Posted on 2014-09-22
3
160 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 78

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 74

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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now