Solved

Convert this code from C# to VB

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 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