Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
need to check if any duplicates in an object properties.
ex:
C#
class Emp{
public string id;
public string Ename;
public string Eadd;
SVM()
{
Emp emp = new Emp(){id=1,Ename="raja",Eadd="1"};
}


now the id and eadd contains duplicates,

how to check duplicates.
}

request:please try to understand ,instead reject any other un useful comments.
Posted
Updated 15-Jan-16 0:30am
v2
Comments
yourfriendaks 15-Jan-16 6:23am    
how you are saying duplicates,as i am seeing two separate properties id and Eadd?
Sinisa Hajnal 15-Jan-16 6:30am    
Are you trying to check for id == Eadd? If yes, what is the problem, just check it in the constructor. If not, describe in more details what is the problem and post relevant code.
rajaChowdare 15-Jan-16 6:40am    
thank you,
i got it

That's complicated, because you would need to check it against all other existing instances - and because each instance is independent of all others, that means you need to keep a static collection and add to it in the class constructor. You can then use that static collection to determine if the new instance is "new" or not.
But that's not as simple as it sounds, because there is no automatic way to tell when an instance has been deleted - you can't use the automatic Dispose as the Garbage Collector will never Dispose any instance as it will always be part of the static collection! So you have to make sure that your consumers always explicitly either Dispose the instance themselves, or call a method to remove it from the static collection when they are finished with it.
To be honest, it's generally more hassle to do something like this, than to make you users actually check if their specific collection is already referencing an identical value. That's trivial: it's just a Linq Where statement!
 
Share this answer
 
Comments
rajaChowdare 15-Jan-16 7:35am    
thank you,
OriginalGriff 15-Jan-16 7:40am    
You're welcome!
If you really need to check if any pair of the object properties hold the same value then write a method to check for such a condition, e.g.
C#
public bool hasDuplicates()
{
  bool has_dup = false;
  if ( id == Ename || id == Eadd || Eadd == Ename) has_dup = true;
  return has_dup;
}
 
Share this answer
 
Comments
rajaChowdare 15-Jan-16 7:35am    
this is a way, thank you,
C#
class Emp
{
    public string id;
    public string Ename;
    public string Eadd;

    public bool HasDuplicates
    {
        get
        {
            if (id == Ename || id == Eadd)
            {
                return true;
            }

            if (Ename == Eadd)
            {
                return true;
            }

            return false;
        }
    }
}


usage

C#
Emp e1 = new Emp { id = "1", Ename = "raja", Eadd = "1" };
Console.WriteLine(e1.HasDuplicates);

Emp e2 = new Emp { id = "2", Ename = "raja", Eadd = "3" };
Console.WriteLine(e2.HasDuplicates);

Emp e3 = new Emp { id = "3", Ename = "raja", Eadd = "raja" };
Console.WriteLine(e3.HasDuplicates);
 
Share this answer
 
The ideal solution would be to detect duplicates as soon as possible as the user enters data, or, later, submits a form, or does whatever to indicate they have finished data entry ... before you even create an instance of an 'Employee class. That's called, in general, "Validation."

But, you could use a special constructor that would throw an error if created/initialized with "wrong" values, and you can use Properties with only a 'Get, and private backing-field variables to increase the security around the data fields:
C#
public class Employee
{
    private string _ename;
    private string _eaddress;
    private string _id;

    public string ID
    {
        get { return _id; }
    }

    public string Ename
    {
        get { return _ename; }
    }

    public string Eaddress
    {
        get { return _eaddress; }
    }

    public Employee(string id, string name, string address)
    {
        if(_id == _ename) throw new ArgumentException("Class Employee: ID and Name cannot be the same !");

        _id = id;
        _ename = name;
        _eaddress = address;
    }

    public void ChangeEmployeeData(string password, string username, string id, string name, string address)
    {
        if (SecurityCheck(password, username))
        {
            if (!String.IsNullOrEmpty(id)) _id = id;
            if (!String.IsNullOrEmpty(name)) _ename = name;
            if (!String.IsNullOrEmpty(address)) _eaddress = address;
        }
        else
        {
            // throw a SecurityException ?
        }
    }

    public bool SecurityCheck(string password, string username)
    {
        return true;
    }
}
Notes:

1. why use Properties: to make serialization easier, to make code more self-documenting

2. again, I'd like to stress that the better way is control the input process so the user never gets the chance to enter "wrong" values; however, it is true that in the real-world sometimes you are dealing with inherited data that you have no control over.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900