Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert this code from C# to VB

Posted on 2004-03-29
4
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Expert Comment

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

Accepted Solution

by:
CyrexCore2k earned 120 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses

719 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