Click here to Skip to main content
15,885,875 members
Home / Discussions / C#
   

C#

 
AnswerRe: Has anyone used CutePDF SDK with C# to prefill forms Pin
BillWoodruff27-Dec-22 18:37
professionalBillWoodruff27-Dec-22 18:37 
QuestionC# Thread Pin
Basel Shikh Osman26-Dec-22 0:11
Basel Shikh Osman26-Dec-22 0:11 
AnswerRe: C# Thread Pin
OriginalGriff26-Dec-22 1:02
mveOriginalGriff26-Dec-22 1:02 
GeneralRe: C# Thread Pin
Basel Shikh Osman26-Dec-22 1:06
Basel Shikh Osman26-Dec-22 1:06 
GeneralRe: C# Thread Pin
OriginalGriff26-Dec-22 1:14
mveOriginalGriff26-Dec-22 1:14 
GeneralRe: C# Thread Pin
BillWoodruff26-Dec-22 2:50
professionalBillWoodruff26-Dec-22 2:50 
AnswerRe: C# Thread Pin
Gerry Schmitz26-Dec-22 8:16
mveGerry Schmitz26-Dec-22 8:16 
QuestionUpdating properties in a IEnumerable Pin
hpjchobbes24-Dec-22 4:14
hpjchobbes24-Dec-22 4:14 
I have an IEnumerable<employee> that I need to update some fields based on the user's security permissions. If a user is in the Admin or HR roles, the CanEdit and CanDelete properties should be set to true. If the user is not Admin or HR, but is their own record, CanEdit should be true. I have come up with two options, and wanted to know which would be considered the better method (or a different way) for speed, readability, and maintainability.

Method 1 first checks if the user is Admin or HR, then loops through the entire collection and updates the values. If the user is not Admin/HR, then it checks each record to see if it is the users and updates. Currently, a user can only have one record, so I quit once I update the matching employee record.
C#
// Admin and HR are allowed to edit and delete all records
if(_userManager.User.IsInRole("Admin") || _userManager.User.IsInRole("HR"))
{
    foreach(var employeeRecord in employeeList)
    {
        employeeRecord.CanEdit = employeeRecord.CanDelete = true;
    }
    return employeeList;
}

// Users are allowed to edit their own record.
foreach(var employeeRecord in employeeList)
{
    if(employeeRecord.WorkEmail.Equals(_userManager.User.Identity.Name, StringComparison.OrdinalIgnoreCase))
    {
        employeeRecord.CanEdit = true;
        return employeeList;
    }
}

// This return is needed in case there is no user record found.
return employeeList;
Method 2 stores a boolean if the user is an Admin/HR, and just sets that as the permission, then checks if the current employeeRecord belongs to the user and sets the permission.
C#
bool hasEditDeletePermission = _userManager.User.IsInRole("Admin") || _userManager.User.IsInRole("HR");

foreach(var employeeRecord in employeeList)
{                
    employeeRecord.CanEdit = employeeRecord.CanDelete = hasEditDeletePermission;
                
    if(_userManager.User.Identity.Name.Equals(employeeRecord.WorkEmail, StringComparison.OrdinalIgnoreCase))
    {
        // Users can edit their own record
        employeeRecord.CanEdit = true;
    }
}

return employeeList;
Method 2 seems shorter and easier to understand, but Method 1 can be faster when the user isn't Admin or HR, which would happen more often. There's also the issue that Method 1 leaves the CanEdit/CanDelete values to whatever they were originally supplied as (currently defaults to false), where as Method 2 will always override any preexisting values which might cause unexpected behavior if other rules are added elsewhere.

Any recommendations on which you would rather use, or even suggestions on better ways to code this would be appreciated.
AnswerRe: Updating properties in a IEnumerable Pin
Gerry Schmitz24-Dec-22 6:35
mveGerry Schmitz24-Dec-22 6:35 
GeneralRe: Updating properties in a IEnumerable Pin
hpjchobbes24-Dec-22 9:58
hpjchobbes24-Dec-22 9:58 
GeneralRe: Updating properties in a IEnumerable Pin
Gerry Schmitz24-Dec-22 23:48
mveGerry Schmitz24-Dec-22 23:48 
GeneralRe: Updating properties in a IEnumerable Pin
BillWoodruff25-Dec-22 5:04
professionalBillWoodruff25-Dec-22 5:04 
AnswerRe: Updating properties in a IEnumerable Pin
Mycroft Holmes24-Dec-22 11:09
professionalMycroft Holmes24-Dec-22 11:09 
Generalc# Pin
Member 1587460624-Dec-22 3:18
Member 1587460624-Dec-22 3:18 
GeneralRe: c# Pin
OriginalGriff24-Dec-22 4:06
mveOriginalGriff24-Dec-22 4:06 
GeneralRe: c# Pin
BillWoodruff24-Dec-22 18:39
professionalBillWoodruff24-Dec-22 18:39 
GeneralRe: c# Pin
OriginalGriff24-Dec-22 19:44
mveOriginalGriff24-Dec-22 19:44 
GeneralRe: c# Pin
RedDk25-Dec-22 6:30
RedDk25-Dec-22 6:30 
GeneralRe: c# Pin
OriginalGriff25-Dec-22 21:44
mveOriginalGriff25-Dec-22 21:44 
GeneralRe: c# Pin
Nitin S26-Dec-22 18:10
professionalNitin S26-Dec-22 18:10 
GeneralRe: c# Pin
RedDk27-Dec-22 8:54
RedDk27-Dec-22 8:54 
Questionconvert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
Sakhalean23-Dec-22 23:48
Sakhalean23-Dec-22 23:48 
AnswerRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
OriginalGriff24-Dec-22 0:29
mveOriginalGriff24-Dec-22 0:29 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
Sakhalean25-Dec-22 4:52
Sakhalean25-Dec-22 4:52 
AnswerRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
BillWoodruff24-Dec-22 18:37
professionalBillWoodruff24-Dec-22 18:37 

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.