I cant seem to find a way not to add objects into my list that are the same, I tried .Contains but I just did not get it to work, any other workarounds? I believe I must use a if statement, and have the creation of the object in the else, but how do I do in the if statement?
public Form1()
{
InitializeComponent();
}
private const string filnamn = "kontakter.bin";
Kontakter kontakt;
private List<Kontakter> kontakter;
private void buttonLagra_Click(object sender, EventArgs e)
{
Kontakter nyKontakt = new Kontakter();
{
nyKontakt.Telefonnr = textBoxTelefonnr.Text;
nyKontakt.Namn = textBoxNamn.Text;
}
kontakter.Add(nyKontakt);
listBox1.DataSource = null;
listBox1.DataSource = kontakter;
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(filnamn, FileMode.OpenOrCreate, FileAccess.Write);
bf.Serialize(fs, kontakter);
fs.Close();
}