2013-08-26 58 views
-2

我有以下問題。具有空值的自動屬性

我的自動屬性SearchedObjClass,SearchedProp,SearchedPropValue導致成空值雖然我分配在主程序值給他們: 有人能幫我找出問題是什麼:

class ADClassNew 
{ 
    public static DirectoryEntry createDirectoryEntry() 
    { 
      string ldapusername = "Username"; 
      string ldapuserpass = "Password"; 

     using (DirectoryEntry root =new DirectoryEntry()) 
     { 
      ADClassNew adclass = new ADClassNew(); 
      root.Path = adclass.LdapPath; 
      root.Username = ldapusername; 
      root.Password = ldapuserpass; 
      root.AuthenticationType = AuthenticationTypes.Secure; 
      return root; 
     } 


    } 

    public string SearchedObjClass { get; set; } 
    public string SearchedProp { get; set; } 
    public string SearchedPropValue { get; set; } 
    public string LdapPath { get; set; } 
    public StringCollection LoadProperties { get; set; } 

    public SearchResult searchDirectory() 
    { 

     DirectoryEntry searchEntry = ADClassNew.createDirectoryEntry(); 
     DirectorySearcher search = new DirectorySearcher(); 
     search.SearchRoot = searchEntry; 
     ADClassNew adclassnew = new ADClassNew(); 

     //string _searchedObjClass = SearchedObjClass; 
     //string _searchedProp = SearchedProp; 
     //string _searchedPropValue = SearchedPropValue; 

     search.Filter = string.Format("(&(ObjectClass={0})({1}={2}))", adclassnew.SearchedObjClass, adclassnew.SearchedProp, adclassnew.SearchedPropValue); 
     //search.Filter = "(&(objectClass=user)(cn=administrator))"; 
     search.PropertiesToLoad.Add("memberof"); 
      SearchResult result = search.FindOne(); 
     return result; 

    } 
} 


public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     ADClassNew adclassnew = new ADClassNew(); 
     adclassnew.LdapPath = "LDAP://MyDomain"; 
     adclassnew.SearchedObjClass = "User"; 
     adclassnew.SearchedProp = "Displayname"; 
     adclassnew.SearchedPropValue = "administrator"; 
    } 
} 
+2

你在哪裏檢查它是空的? –

回答

1

你不」 t 任何與在窗體構造函數中創建的對象 - 它只是超出範圍,將被收集。您是否期望值持續存在全部ADClassNew的實例?如果是這樣那麼使用static屬性:

public static string SearchedObjClass { get; set; } 
public static string SearchedProp { get; set; } 
public static string SearchedPropValue { get; set; } 
public static string LdapPath { get; set; } 

然後使用theClass描述名稱設置他們在您的初始化,而不是一個實例:

ADClassNew.LdapPath = "LDAP://MyDomain"; 
ADClassNew.SearchedObjClass = "User"; 
ADClassNew.SearchedProp = "Displayname"; 
ADClassNew.SearchedPropValue = "administrator"; 

或者,也可以使對象的形式重新使用的屬性它:

public partial class Form1 : Form 
{ 
    private ADClassNew _adClassNew {get; set;} 

    public Form1() 
    { 
     InitializeComponent(); 

     _adclassnew = new ADClassNew(); 
     _adclassnew.LdapPath = "LDAP://MyDomain"; 
     _adclassnew.SearchedObjClass = "User"; 
     _adclassnew.SearchedProp = "Displayname"; 
     _adclassnew.SearchedPropValue = "administrator"; 
    } 
} 
+0

非常感謝您的幫助......它的工作原理。 –