'System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<string>>.ContainsValue(System.Collections.Generic.List<string>)' has invalid arguments

Posted on 2007-10-12
Last Modified: 2008-01-09
I can't see what I"m doing wrong here when I try passing the generic string to the ContainsValue method:

1            public bool Authorize(IPrincipal principal, List<string> rightsRequested)
2            {
3                bool result = false;
5                if (principal == null) throw new System.ArgumentNullException("IPrincipal is null");
6                if (rightsRequested.Count < 1) throw new System.ArgumentNullException("rightsRequested generic string is null");
8                if ((rightsRequested.Count == 1) && (dicRights.ContainsKey(principal.Identity.Name)))
9                    result = dicRights.ContainsValue(rightsRequested[0]);
11               return result;
12           }
Question by:dba123
    LVL 20

    Accepted Solution

    This accepted solution here may help

    LVL 25

    Assisted Solution

    You've got several questions on this that I've seen, so I'll try and outline the issue:

    1) dicRights is a key-value dictionary where the key is a string and the value is a List<string>
    2) dicRights.ContainsKey(key) will return true if the key value is found
    3) dicRights[key] will return the LIST of strings, so to do it in one call you'll need to do

    result = dicRights[key].Contains(rightsRequested[0]);
    LVL 22

    Expert Comment

    dstanley9 is correct, but if you search for a key that is not in the dictionary, this will throw a null reference exception. The following wiil not:

    bool result = false;
    List<string> current = dicRights[principal.Identity.Name];
    if (current != null)
       result = current.Contains(rightsRequested[0]);

    LVL 1

    Author Comment

    I found out that it was trying to compare two reference types, not 2 strings which is why it wasn't matching the string when looking up.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now