Click here to Skip to main content
15,893,588 members
Home / Discussions / C#
   

C#

 
GeneralRe: listBox Item Removal Pin
V.22-Oct-12 1:27
professionalV.22-Oct-12 1:27 
AnswerRe: listBox Item Removal Pin
BobJanova22-Oct-12 4:22
BobJanova22-Oct-12 4:22 
GeneralRe: listBox Item Removal Pin
electriac22-Oct-12 5:48
electriac22-Oct-12 5:48 
GeneralRe: listBox Item Removal Pin
DaveyM6922-Oct-12 11:41
professionalDaveyM6922-Oct-12 11:41 
GeneralRe: listBox Item Removal Pin
electriac23-Oct-12 10:22
electriac23-Oct-12 10:22 
GeneralRe: listBox Item Removal Pin
DaveyM6923-Oct-12 22:36
professionalDaveyM6923-Oct-12 22:36 
GeneralRe: listBox Item Removal Pin
BobJanova24-Oct-12 6:27
BobJanova24-Oct-12 6:27 
GeneralRe: listBox Item Removal Pin
DaveyM6924-Oct-12 21:30
professionalDaveyM6924-Oct-12 21:30 
Yes, that was deliberate as the OP was wanting reference checking so two identical strings were treated as different objects. Even with those added it behaves as expected - it's just the interning of the string class that screws it!
Example below. Warning: No null checking on value
C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // listBox added in designer
        // buttonRemove added in designer
        // buttonRemove Click handler added in designer
        public Form1()
        {
            InitializeComponent();
            listBox.Items.Add(new MyString(" "));
            listBox.Items.Add(new MyString("2"));
            listBox.Items.Add(new MyString(" "));
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            if (listBox.SelectedItem != null)
                listBox.Items.Remove(listBox.SelectedItem);
        }
    }
}

public class MyString : IEquatable<MyString>
{
    private string value;

    public MyString(string value)
    {
        this.value = value;
    }

    public static implicit operator MyString(string value)
    {
        return new MyString(value);
    }
    public static implicit operator string(MyString myString)
    {
        return myString.value;
    }
    public static bool operator ==(MyString first, MyString other)
    {
        if (object.ReferenceEquals(first, other))
            return true;
        if (object.ReferenceEquals(null, first) || object.ReferenceEquals(null, other))
            return false;
        return first.value == other.value;
    }
    public static bool operator !=(MyString first, MyString other)
    {
        return !(first == other);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as MyString);
    }
    public bool Equals(MyString other)
    {
        if (object.ReferenceEquals(null, other))
            return false;
        return value.Equals(other.value);
    }
    public override int GetHashCode()
    {
        return value.GetHashCode();
    }
    public override string ToString()
    {
        return value;
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: listBox Item Removal Pin
electriac24-Oct-12 12:35
electriac24-Oct-12 12:35 
AnswerRe: listBox Item Removal Pin
Braj_1223-Oct-12 2:36
Braj_1223-Oct-12 2:36 
GeneralRe: listBox Item Removal Pin
electriac23-Oct-12 10:24
electriac23-Oct-12 10:24 
Questionconstructor call Pin
anshumansingh7121-Oct-12 20:24
anshumansingh7121-Oct-12 20:24 
AnswerRe: constructor call PinPopular
J4amieC21-Oct-12 21:32
J4amieC21-Oct-12 21:32 
QuestionHow to Send & Receive data using USB port in C# Pin
willington.d21-Oct-12 19:35
willington.d21-Oct-12 19:35 
AnswerRe: How to Send & Receive data using USB port in C# Pin
Richard MacCutchan21-Oct-12 22:00
mveRichard MacCutchan21-Oct-12 22:00 
GeneralRe: How to Send & Receive data using USB port in C# Pin
willington.d21-Oct-12 23:02
willington.d21-Oct-12 23:02 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Richard MacCutchan21-Oct-12 23:24
mveRichard MacCutchan21-Oct-12 23:24 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Dave Kreskowiak22-Oct-12 2:08
mveDave Kreskowiak22-Oct-12 2:08 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 5:07
protectorMarco Bertschi22-Oct-12 5:07 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Dave Kreskowiak22-Oct-12 10:25
mveDave Kreskowiak22-Oct-12 10:25 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 21:35
protectorMarco Bertschi22-Oct-12 21:35 
AnswerRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 5:02
protectorMarco Bertschi22-Oct-12 5:02 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Richard Andrew x6422-Oct-12 7:48
professionalRichard Andrew x6422-Oct-12 7:48 
SuggestionRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 21:43
protectorMarco Bertschi22-Oct-12 21:43 
QuestionError: Calling C++ dll function in C# Pin
taibc21-Oct-12 17:33
taibc21-Oct-12 17:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.