I have a class "Class1" that has a string variable "sText" in .NET 2.0. I have created a list of object of that class "lstClass1". It stores many objects of that class after setting its string variable.
Complete code is:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!lstClass1.Contains(new Class1("text1")))
lstClass1.Add(new Class1("text1"));
}
public List<Class1> lstClass1 = new List<Class1>();
}
public class Class1
{
public Class1(string sText)
{
this.sText = sText;
}
public string sText = "";
}
Now, the problem is that I only want to add class objects that has string with unique text value. For example, if lstClass1 already has an object with string variable value "text1" then it should not allow addition of an object that also has "text1". So, I wrote code:
if (!lstClass1.Contains(new Class1("text1")))
lstClass1.Add(new Class1("text1"));
But it always allows the text "text1" to be added even when there is already an object in the list with "text1" string. My assumption was that on first button click event "button1_Click" the object will be added because the list is empty but on next button click the List.Contains function will check if there is already an object in the list with string variable "text1" and if found then it will not be added. But it always allow to add object with text "text1" even if it is already present in the list.
Please note: I have not taken a simple list of strings or an of string because I want to explain my large problem of list, classes and objects in a simple way.