Solved

Convert this code from C# to VB

Posted on 2004-03-29
4
208 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now