Ivan Zhivolupov Ivan Zhivolupov -4 years ago 129
C# Question

Pass Many Classes To Method

Good time of the day!

I'm trying to pass few classes, which inherit from one base class.

The structure looks as follows:


  • Permission Class - Permisions for user


    • Edit : Permission - Ability to edit record

    • Delete : Permission - Ability to delete record

    • Create : Permission - Ability to create record


  • User Class - User class


    • private Permission[] permissions - permissions available for user

    • public void AddDefaultPermissions(Permission[] permissions)




What i can't figure out, is how to pass multiple permissions to this function and check if permission has already been added. What i was thinking of is to allow something like the following method call:

AddDefaultPermissions(Edit, Delete, Create);


Permission Class:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Permission {

private int permissionID;
private string permissionName;
private string permissionDescription;
private int accessStartTimestamp;
private int accessEndTimestamp;

public int PermissionID {
get { return permissionID; }
set { permissionID = value; }
}

public string PermissionName {
get { return permissionName; }
set { permissionName = value; }
}

public string PermissionDescription {
get { return permissionDescription; }
set { permissionDescription = value; }
}

public int AccessStartTimestamp {
get { return accessStartTimestamp; }
set { accessStartTimestamp = value; }
}

public int AccessEndTimestamp {
get { return accessEndTimestamp; }
set { accessEndTimestamp = value; }
}
}


Edit Class:

using System;
using System.Collections;
using System.Collections.Generic;

public class Edit : Permission {

public Edit() {
PermissionID = 1;
PermissionName = "Edit";
PermissionDescription = "Allow user to edit records";
AccessStartTimestamp = 1488369600;
AccessEndTimestamp = 1491048000;
}

}


For checking if permission is already exists, PermissionID variable can be used. This variable is inherited by all of the permission classes and unique for the permission (Edit = 1, Create = 2, Delete = 3);

I saw some examples with LINQ, but couldn't make it to work, since actually never worked with LINQ.
Is it possible to achieve this functionality + to check if permission has already been added to avoid duplicates?

Answer Source

You can easily eliminate the duplicaets from your input using Distinct:

AddDefaultPermissions(params Permission[] permissions) 
{
    var actualPermissions = permissions.Distinct();
}

This assumes your Permission-class implements Equals and GetHashCoe appropriately by comparing their PermissionID-property, e.g:

class Permission
{
    public override bool Equals(object obj)
    {
        var p = obj as Permission;
        if(p == null) return false;
        return this.PermissionID == p.PermissionID;
    }
    public override int GetHashCode()
    {
        return this.PermissionID .GetHashCode();
    }
}

If you can´t change the implementation of this class you can also use your own implementation for IEqualityComparer within Distinct:

var actualPermissions = permissions.Distinct(myEqualityComparer);

If you just want to check if the item was allready added you can just check for a Contains which will rely on Equals to be implemented as shown above:

AddDefaultPermissions(params Permission[] permissions) 
{
    var actualPermissions = permissions.Distinct();
    foreach(var p in permissions)
        if(!listOfAllreadyAddedPermissions.Contains(p)
            listOfAllreadyAddedPermissions.Add(p);
}

Or alternativly again - if you don´t want to change the class - use Any:

if(!listOfAllreadyAddedPermissions.Any(x => x.PermissionId == p.PermissionId)
    listOfAllreadyAddedPermissions.Add(p);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download