Click here to Skip to main content
15,915,081 members
Home / Discussions / C#
   

C#

 
QuestionWebService - WSDL choice element Pin
marosprog22-Oct-12 20:27
marosprog22-Oct-12 20:27 
QuestionMigrating Screwturn Wiki content to Sharepoint wiki Pin
chuckdawit22-Oct-12 9:21
chuckdawit22-Oct-12 9:21 
QuestionNeed assistance with Excel and datagrid Pin
yoni.kess22-Oct-12 1:35
yoni.kess22-Oct-12 1:35 
AnswerRe: Need assistance with Excel and datagrid Pin
Richard MacCutchan22-Oct-12 3:27
mveRichard MacCutchan22-Oct-12 3:27 
GeneralRe: Need assistance with Excel and datagrid Pin
yoni.kess22-Oct-12 6:47
yoni.kess22-Oct-12 6:47 
GeneralRe: Need assistance with Excel and datagrid Pin
Richard MacCutchan22-Oct-12 7:31
mveRichard MacCutchan22-Oct-12 7:31 
GeneralRe: Need assistance with Excel and datagrid Pin
yoni.kess23-Oct-12 0:02
yoni.kess23-Oct-12 0:02 
GeneralRe: Need assistance with Excel and datagrid Pin
Richard MacCutchan23-Oct-12 0:18
mveRichard MacCutchan23-Oct-12 0:18 
QuestionlistBox Item Removal Pin
electriac22-Oct-12 0:42
electriac22-Oct-12 0:42 
AnswerRe: listBox Item Removal Pin
electriac22-Oct-12 1:07
electriac22-Oct-12 1:07 
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 

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.