Solved

Convert this code from C# to VB

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
vb6 connector to mongodb 2 162
Zip Folders Using Chilkat Routines 1 86
Win 10 Automation Error with .doCmd in Access 2 90
VB6 Enviroment 3 35
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

734 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