Solved

Convert this code from C# to VB

Posted on 2004-03-29
4
207 Views
Last Modified: 2010-05-18
public string GetGroups()
{
  DirectorySearcher search = new DirectorySearcher(_path);
  search.Filter = "(cn=" + _filterAttribute + ")";
  search.PropertiesToLoad.Add("memberOf");
  StringBuilder groupNames = new StringBuilder();
  try
  {
    SearchResult result = search.FindOne();
    int propertyCount = result.Properties["memberOf"].Count;
    String dn;
    int equalsIndex, commaIndex;

    for( int propertyCounter = 0; propertyCounter < propertyCount;
         propertyCounter++)
    {
      dn = (String)result.Properties["memberOf"][propertyCounter];

      equalsIndex = dn.IndexOf("=", 1);
      commaIndex = dn.IndexOf(",", 1);
      if (-1 == equalsIndex)
      {
        return null;
      }
      groupNames.Append(dn.Substring((equalsIndex + 1),
                        (commaIndex - equalsIndex) - 1));
      groupNames.Append("|");
    }
  }
  catch(Exception ex)
  {
    throw new Exception("Error obtaining group names. " +
      ex.Message);
  }
  return groupNames.ToString();
}

0
Comment
Question by:luckyinc
  • 2
4 Comments
 
LVL 7

Expert Comment

by:ramesh12
ID: 10708983
0
 
LVL 7

Expert Comment

by:ramesh12
ID: 10708990
Are you asking for vb or vb.net
0
 
LVL 14

Accepted Solution

by:
CyrexCore2k earned 30 total points
ID: 10709405
Without knowing too much about all the objects you happen to be using this is what I came up with.

- Begin Code -
    Public Function GetGroups() As String
        Dim search As DirectoryServices.DirectorySearcher
        Dim groupNames As System.Text.StringBuilder
        Dim result As DirectoryServices.SearchResult
        Dim propertyCount, propertyCounter, equalsIndex, commaIndex As Int32
        Dim dn As String

        search = New DirectoryServices.DirectorySearcher(path)
        groupNames = New System.Text.StringBuilder()

        search.Filter = "(cn=" & filterAttribute & ")"
        search.PropertiesToLoad.Add("memberOf")
        Try
            result = search.FindOne()
            propertyCount = result.Properties("memberOf").Count
            For propertyCounter = 1 To (propertyCount - 1)
                dn = result.Properties("memberOf")(propertyCounter)

                equalsIndex = dn.IndexOf("=", 1)
                commaIndex = dn.IndexOf(",", 1)
                If -1 = equalsIndex Then
                    GetGroups = System.DBNull.Value.ToString
                    Exit Function
                End If
                groupNames.Append(Mid(dn, (equalsIndex + 1), (commaIndex - equalsIndex) - 1))
                groupNames.Append("|")
            Next
        Catch e As Exception
            Throw New Exception("Error obtaining group names. " & e.Message)
        End Try
        GetGroups = groupNames.ToString()
    End Function
- End Code -

Hope this helps! :)

0
 

Author Comment

by:luckyinc
ID: 10713899
Thanks!!! Worked Great!!  I might have one more for you to do later today!!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

18 Experts available now in Live!

Get 1:1 Help Now