Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I wrote a class file and calling the class file thru my console application.

I got an error on my console application

"can't implictly convert type 'ActiveDir.employee to string"

Console Application code :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace activeDirectoryLdapExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter user: ");
            String username = Console.ReadLine();
            ActiveDir.ActiveDirSearch search = new ActiveDir.ActiveDirSearch();
            string output = search.SearchUser(username);

        }

    }
}



Class File Code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ActiveDir
{
    public class Employee
{
    // instance variables 
     public String name;
    public String surname;
     public string title;
        //   ...
    // Constructors
    public Employee()
    {
    }
}

    public class ActiveDirSearch
    {
        public ActiveDirSearch()
        { }

        public Employee SearchUser(string username)
        {
            
            Employee employee = new Employee();

            if (username.Contains("."))
            {
                string[] splitString = username.Split(new char[] { '.' });
                username = splitString[splitString.Length - 1];
            }


            try
            {
                DirectoryEntry entry = new DirectoryEntry("GC://Test", "Test\\Test", "Ld@Test", AuthenticationTypes.Secure);

                // DirectorySearcher search = new DirectorySearcher(myLdapConnection);
                System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);
                //  search.Filter = "(cn=" + username + ")";

                if (username.IndexOf(" ") > 0)
                    search.Filter = "(cn=" + username + ")";
                else
                    search.Filter = "(sn=" + username + ")";

                SearchResult result = search.FindOne();


                if (result != null)
                {

                    // user exists, cycle through LDAP fields (cn, telephonenumber etc.)

                    ResultPropertyCollection fields = result.Properties;

                    foreach (String ldapField in fields.PropertyNames)
                    {
                        // cycle through objects in each field e.g. group membership
                        // (for many fields there will only be one object such as name)                        
                        foreach (Object myCollection in fields[ldapField])
{
     if (ldapField == "name") 
         employee.name = myCollection.ToString();
     if (ldapField == "surname")
         employee.surname = myCollection.ToString();
    if(ldapField=="title")
        employee.title=myCollection.ToString();
    
}
                    }

                }

                else
                {
                   return null;
               }
            }

            catch (Exception e)
            {
                throw e;
            }

            return employee ;
        }
    }
}


did i miss something here ?
Posted

The class ActiveDir.Employee is your own class, so what's the problem? It is certainly not a string and not converted to string implicitly.

You may want to override object.ToString in your declaration of the class Employee and call it where string is required. Another way is to introduce implicit String operator for this class.

Something like that:
C#
class Employee {
    public override string ToString() {
        return string.Format("{0} {1} {2}", title, name, surname).Trim();
    }
    public static implicit operator String(Employee value) {
        return value.ToString();
    }
}


I used string.Trim to handle the cases if title is empty, etc.; you can improve it do your liking.

—SA
 
Share this answer
 
v5
Comments
shan1395 22-Feb-11 19:15pm    
Thanks SA
shan1395 22-Feb-11 20:14pm    
Hi SA,
I gotta few more things to add up my class library file.

Like "Retrieve all users"
"Retrieve users based on OU"
"Retrieve all terminated user based on a start date and also on date range"
shan1395 22-Feb-11 20:34pm    
what is the best approach to do that,any help much appreciated.
Hi public Employee SearchUser(string username); is the signature of your SearchUser method.

It returns an Employee object, which I'm sure you are aware is not a string, and therefore cannot be assigned to a string.

What you should do is create a method or property within the Employee class that returns a string in the format you want.
e.g.
C#
public class Employee {
  // instance variables
  public String name;
  public String surname;
  public string title;

  public String FullName() {
    return String.Format("{0}. {1} {2}", title, name, surname);
  }
}

and then
string output = search.SearchUser(username).FullName();


Alan.
 
Share this answer
 
Comments
shan1395 22-Feb-11 17:28pm    
Hi Alan,

How can i handle the Null,if user is not in the AD then am returning NULL,and NULL is returning it throws eception.

How can i handle this ?any help appreciated

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