locked
Please Help Me Translate This C# to Vb.net RRS feed

  • Question

  • User-786564416 posted

    Please help me translate the bold part to VB.NET

    List<User> users = new List<User>();
    try
    {
        DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
        root = new DirectoryEntry("LDAP://" + root.Properties["defaultNamingContext"][0]);
        DirectorySearcher search = new DirectorySearcher(root);
        search.Filter = "(&(objectClass=user)(objectCategory=person))";
    
        search.PropertiesToLoad.Add("samaccountname");
        search.PropertiesToLoad.Add("displayname");
        search.PropertiesToLoad.Add("mail");
        search.PropertiesToLoad.Add("telephoneNumber");
        search.PropertiesToLoad.Add("department");
        search.PropertiesToLoad.Add("title");
    
        SearchResultCollection results = search.FindAll();
        if (results != null)
        {
            foreach (SearchResult result in results)
            {
                foreach (DictionaryEntry property in result.Properties)
                {
                    Debug.Write(property.Key + ": ");
                    foreach (var val in (property.Value as ResultPropertyValueCollection)) { 
                        Debug.Write(val +"; ");
                    }
                    Debug.WriteLine("");
                }
            }
        }
    }
    catch (Exception ex)
    {
    
    }

    The problem I faced is specially on the statement: result != null . I tried to translate it as result is not null but I got error (null is no longer used)

    Monday, October 2, 2017 8:39 AM

All replies

  • User-2010311731 posted

    Here is a great converter tool...

    http://converter.telerik.com/

    Matt

    Monday, October 2, 2017 3:58 PM
  • User1396448631 posted

    Try

    If result Is Nothing

    Monday, October 2, 2017 7:19 PM
  • User-786564416 posted

    Thanks a lot. The translation tool gave me the following:

    If results IsNot Nothing Then
        For Each result As SearchResult In results
            For Each [property] As DictionaryEntry In result.Properties
                 Debug.Write([property].Key + ": ")
                 For Each val As var In TryCast([property].Value, ResultPropertyValueCollection)
                      Debug.Write(val + "; ")
                 Next
                 Debug.WriteLine("")
            Next
        Next
    End If

    It generates error on the (val) as: not defined. I tried to replace it by variable but not available. How to rectify it? Also, is the translation is accurate?

    Monday, October 2, 2017 8:06 PM
  • User2103319870 posted

    It generates error on the (val) as: not defined. I tried to replace it by variable but not available. How to rectify it?

    You can try with the below code

     If results IsNot Nothing Then
                For Each result As SearchResult In results
                    For Each prop As DictionaryEntry In result.Properties
                        Debug.Write(prop.Key + ": ")
                        'For Each val As var In TryCast([property].Value, ResultPropertyValueCollection)
                        '    Debug.Write(val + "; ")
                        'Next
                        For Each propertyVal As Object In prop.Value
                            Debug.Write(prop.Key + "; " + propertyVal)
                        Next
                        Debug.WriteLine("")
                    Next
                Next
            End If

    Monday, October 2, 2017 8:37 PM
  • User-707554951 posted

    Hi  alihusain_77,

    C# uses 'var' for implicit typing - VB uses Option Infer On combined with omitting the type. The VB equivalent is.

    Option Infer On
    Public Class WebForm1
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim users As New List(Of User)()
            Try
                Dim root As New DirectoryEntry("LDAP://RootDSE")
                root = New DirectoryEntry("LDAP://" + root.Properties("defaultNamingContext")(0))
                Dim search As New DirectorySearcher(root)
                search.Filter = "(&(objectClass=user)(objectCategory=person))"
    
                search.PropertiesToLoad.Add("samaccountname")
                search.PropertiesToLoad.Add("displayname")
                search.PropertiesToLoad.Add("mail")
                search.PropertiesToLoad.Add("telephoneNumber")
                search.PropertiesToLoad.Add("department")
                search.PropertiesToLoad.Add("title")
    
                Dim results As SearchResultCollection = search.FindAll()
                If results IsNot Nothing Then
                    For Each result As SearchResult In results
                        For Each [property] As DictionaryEntry In result.Properties
                            Debug.Write([property].Key + ": ")
                            For Each va In TryCast([property].Value, ResultPropertyValueCollection)
                                Debug.Write(va() + "; ")
                            Next va
                            Debug.WriteLine("")
                        Next
                    Next
                End If
    
            Catch ex As Exception
            End Try
    
        End Sub
    
    End Class

    For more information, please refer to the following links:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-infer-statement

    You also could try to use object:

     For Each va As Object
                                In TryCast([property].Value, ResultPropertyValueCollection)
                                Debug.Write(va() + "; ")
                            Next

    Best regards

    Cathy

    Tuesday, October 3, 2017 6:15 AM