How to get my SortedList<TKey, TValue> C# to compile.

Hi,

I am having problems with the code below. I want to know how I can get it to compile.

The problem is in the last few lines.

            foreach (KeyValuePair<string,AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.ObjectGUID, ou.DN);
            }
            Console.WriteLine("");

The completed code is below.

Thanks,

Ward
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Collections;

namespace ADSort1
{

    class AD_OU
    {
        public string DN = null;
        public string ObjectGUID = null;
    }

    class Program
    {
        static SortedList<string,AD_OU> OU_List = new SortedList<string,AD_OU>();

        static string fnGet_Object_Category(string starting_path, string filter, string append_condition)
        {
            string category = null;

            DirectoryEntry BaseOU = new DirectoryEntry(starting_path);
            DirectorySearcher searcher = new DirectorySearcher(BaseOU, filter);
            //fnSetSearcher_Settings(ref searcher);
            searcher.PropertiesToLoad.Add("objectCategory");

            SearchResult result = searcher.FindOne();

            category = result.Properties["objectCategory"][0].ToString();
            // "(&(objectCategory=person)(objectClass=user))";
            if (append_condition == "" || append_condition == null)
            {
                category = "(objectCategory=" + category + ")";
            }
            else
            {
                category = "(&(objectCategory=" + category + ")" + append_condition + ")";
            }

            return category;
        }
        // -------------------------------------------------------------------
        // fnSetSearcher_Settings: Set the searcher settings.
        // -------------------------------------------------------------------

        public static void fnSetSearcher_Settings(ref DirectorySearcher searcher)
        {
            searcher.PageSize = 1000;
            searcher.SizeLimit = 10000000;
            searcher.ServerTimeLimit = new TimeSpan(2, 0, 0);
            searcher.ClientTimeout = new TimeSpan(2, 0, 0);
            // searcher.PropertyNamesOnly = true;
        }

        static void Main(string[] args)
        {
            string dn;
            string objectGUID;

            string starting_path = "LDAP://DC=acme,DC=net";
            DirectoryEntry BaseOU = new DirectoryEntry(starting_path);
            string filter = "(objectClass=organizationalUnit)";

            filter = fnGet_Object_Category(starting_path, filter, "");

            DirectorySearcher searcher = new DirectorySearcher(BaseOU, filter);

            fnSetSearcher_Settings(ref searcher);

            searcher.PropertiesToLoad.Add("objectGUID");
            searcher.PropertiesToLoad.Add("distinguishedName");
            SearchResultCollection ad_results = searcher.FindAll();
            foreach (SearchResult result in ad_results)
            {
                dn = result.Properties["distinguishedName"][0].ToString();
                objectGUID = new Guid((byte[])result.Properties["objectGUID"][0]).ToString();
                AD_OU found_ou = new AD_OU();
                found_ou.DN = dn;
                found_ou.ObjectGUID = objectGUID;
                OU_List.Add(objectGUID, found_ou);
            }
            ad_results.Dispose();
            searcher.Dispose();
            Console.WriteLine("");
            foreach (KeyValuePair<string,AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.ObjectGUID, ou.DN);
            }
            Console.WriteLine("");
        }
    }
}

Open in new window

LVL 1
whorsfallAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
foreach (KeyValuePair<string, AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.Value.ObjectGUID, ou.Value.DN);
            }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AshokSr. Software EngineerCommented:

            foreach (KeyValuePair<string, AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.Key, ou.Value);
            }
            Console.WriteLine("");

HTH
Ashok
0
Miguel OzSoftware EngineerCommented:
         foreach (KeyValuePair<string, AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.Key, ou.Value.DN);
            }
0
Mahmoud_Al_HattabCommented:
Dear whorsfall,

 The best solutiuon is SriVaddadi's one. ashok111 will generate the ou,Value,ToString() to return the type of AD_OU. mas_oz2003's answer works fine as well.

 The explanation why is that the KeyValuePair object in the list holds Key and Value properties; the key holds the string when inserted into it, and the Value is the object itself which is the AD_OU object.

 You could have used this statement as well:
OU_List.Values.ToList().ForEach(ou=>Console.WriteLine("{0} {1}", ou.ObjectGUID, ou.DN));

by this new line using lambda expressions you are accessing the AD_OU objects directly not through the KeyValuePair objects.

See the diffenence with this:
OU_List.ToList().ForEach(kvp => Console.WriteLine("{0} {1}", kvp.Value.ObjectGUID, kvp.Value.DN));

if you still need any further queries please do not hesitate.

regards,
Mahmoud Al-Hattab
0
whorsfallAuthor Commented:
Excellent info
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.