Solved

Passing variable in txt box..

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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