Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:06:25+00:00 2026-05-31T17:06:25+00:00

Simple class public class Group { public Int16 ID { get; private set; }

  • 0

Simple class

public class Group
{
    public Int16 ID { get; private set; }
    public string Name { get; set; }
    public Group ( Int16 id, string name ) 
    { ID = id; Name = name; }
}

What I would like is an ObservableCollection where the collection forces uniqueness on ID and CaseInsensitive uniqueness on Name.

What I tried is:

public class Group
{
    public Int16 ID { get; private set; }
    public string Name { get; set; }

    public override bool Equals(System.Object obj)
    {
        // If parameter is null return false.
        if (obj == null)
        {
            return false;
        }

        // If parameter cannot be cast to Point return false.
        Group g = obj as Group;
        if ((System.Object)g == null)
        {
            return false;
        }

        // Return true if either fields match:
        return ( ID == g.ID || string.Compare(Name, g.Name, true) == 0 ) ;
    }

    public bool Equals(Group g)
    {
        // If parameter is null return false:
        if ((object)g == null)
        {
            return false;
        }

        // Return true if either fields match:
        return ( ID == g.ID || string.Compare(Name, g.Name, true) == 0 ) ;
    }

    public override int GetHashCode()
    {
        return ID; // ^ (Int32)Name.ToLower();
    }

    public Group ( Int16 id, string name ) 
    { ID = id; Name = name; }
}

HashSet &lt Group &gt

That prevents adding a group with the same ID not doe not prevent adding a group with the same Name. And it does not stop renaming a Name to an existing Name.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:06:27+00:00Added an answer on May 31, 2026 at 5:06 pm

    It will take a little tweaking of your class but here is how to do it. First you need group to notify the collection that you are about to change the name, do this by adding a event and modifying the setter of Name.

    First Add this Interface and event handeler.

    public delegate void TestForColisions(object sender, TestForColisionsArgs e);
    
    public class TestForColisionsArgs : CancelEventArgs
    {
        public TestForColisionsArgs(object newValue)
        {
            NewValue = newValue;
        }
    
        public object NewValue { get; private set; }
    }
    
    
    public interface ITestForColisions
    {
        /// <summary>
        /// Set the event to Canceled if there will be a collision.
        /// </summary>
        event TestForColisions TestForCollision;
    }
    

    Then have your class implement the interface

    public class Group : ITestForColisions, IEquateable<Group>
    {
        public Int16 ID { get; private set; }
    
        private string _Name;
        public string Name 
        {
            get { return _Name; }
            set
            {
                //If RaiseNameChanging returns true there was a collision.
                if (RaiseNameChanging(value))
                {
                    throw new ArgumentException(String.Format("The name {0} is in use in the collection", value));
                }
                else
                {
                    _Name = value;
                }
            }
        }
    
        protected virtual bool RaiseNameChanging(string name)
        {
            //Make a copy with the new name.
            var newGroup = (Group)this.MemberwiseClone();
            newGroup.Name = name;
    
            var cancelEventArgs = new TestForColisionsArgs(newGroup);
            if (TestForCollision != null)
            {
                TestForCollision(this, cancelEventArgs);
            }
            return cancelEventArgs.Cancel;
        }
    
       //(...)
    }
    

    Then you will need a custom collection that listens for TestForCollision events and handles accordingly. For most of the methods you can just call the parent _BaseSet‘s version, however for any of the methods that modify the set you will need to either subscribe or un-subscribe to the event. I have done Clear, Add and Remove for you.

    public class ColisionTestedCollection<T> : ISet<T>
        where T : ITestForColisions
    {
        public ColisionTestedCollection(ISet<T> baseSet)
        {
            _BaseSet = baseSet;
            _EqualityComparer = EqualityComparer<T>.Default;
        }
    
        public ColisionTestedCollection(ISet<T> baseSet, IEqualityComparer<T> equalityComparer)
        {
            _BaseSet = baseSet;
            _EqualityComparer = equalityComparer;
        }
    
        private ISet<T> _BaseSet;
        private IEqualityComparer<T> _EqualityComparer;
    
    
        void TestItemsForCollision(object sender, TestForColisionsArgs e)
        {
            if (_BaseSet.Contains((T)e.NewValue, _EqualityComparer))
            {
                e.Cancel = true;
            }
        }
    
        public bool Add(T item)
        {
            bool added = _BaseSet.Add(item);
            if(added)
                item.TestForCollision += TestItemsForCollision;
            return added;
        }
    
        void ICollection<T>.Add(T item)
        {
            ((ICollection<T>)_BaseSet).Add(item);
            item.TestForCollision += TestItemsForCollision;
        }
    
        public bool Remove(T item)
        {
            bool removed = _BaseSet.Remove(item);
            if (removed)
                item.TestForCollision -= TestItemsForCollision;
            return removed;
        }
    
        public void Clear()
        {
            foreach (var item in _BaseSet)
                item.TestForCollision -= TestItemsForCollision;
            _BaseSet.Clear();
        }
    
        public void ExceptWith(IEnumerable<T> other)
        {
            throw new NotImplementedException();
        }
    
        public void IntersectWith(IEnumerable<T> other)
        {
            throw new NotImplementedException();
        }
    
        public void SymmetricExceptWith(IEnumerable<T> other)
        {
            throw new NotImplementedException();
        }
    
        public void UnionWith(IEnumerable<T> other)
        {
            throw new NotImplementedException();
        }
    
        //The rest of the functions will just be simply calling _BaseSet's version of the method. 
        public bool IsProperSubsetOf(IEnumerable<T> other)
        {
            return _BaseSet.IsProperSubsetOf(other);
        }
        //(snip)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a simple class [Serializable] public class MyClass { public String FirstName { get;
Given a simple class: public class Person { public string FirstName; public string LastName;
For example I have a simple class like public class Person{ public int Age
I have a simple object in the format of class MyObject { public string
Assuming I have a simple structure that looks like this: public class Range {
public class GroupWithSpecificOptionsNotFoundException : Exception { public GroupWithSpecificOptionsNotFoundException(string place, Dictionary<string, string> options) : base(string.Format(Group
I have the simple class using auto-implemented properies: Public Class foo { public foo()
I've got a simple class defined as: public class MyClass { //Some properties public
If I have a simple class such as:- @XmlRootElement public class MyClass { @XmlAttribute(required=true)
Suppose I have the following (trivially simple) base class: public class Simple { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.