user3569698
user3569698

Reputation:

Cannot refer to an instance member of a class from a shared method

How you get a public shared function outside of a Protected Sub, use the values from within a protected sub to postBack to the same webpage. The postback reply works, but the query of the function fails at (Line 44 Char 17 "fqdom = dom & ".forest.local")

Imports System
Imports System.IO
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices.ActiveDirectory

Partial Class _Default 
 Inherits System.Web.UI.Page

Dim dom As String
Dim Group1 As String
Dim Group2 As String
Dim usrname As String
Dim fqdom As String
Dim netdom As String

Private Function GetDataFromArrayList() As ArrayList
    Dim DomainList As New ArrayList()
    DomainList.Add(New ListItem("d1", "dom1"))
    DomainList.Add(New ListItem("d2", "dom2"))
    Return DomainList
End Function

Protected Sub Selection_Changed(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then
        For Each item As ListItem In GetDataFromArrayList()
            DropDownList1.Items.Add(item)
        Next
    End If
End Sub

Public Shared Function GetGroups() As ArrayList
    Dim groupList As New ArrayList()
    Dim usrname As String
    Dim fqdom As String
    'Dim netdom As String
    Dim groupCheck As String
    fqdom = dom & ".forest.local"

    Dim entry As System.DirectoryServices.DirectoryEntry
    Dim searcher As System.DirectoryServices.DirectorySearcher
    Dim result As System.DirectoryServices.SearchResult

    Try
        entry = New System.DirectoryServices.DirectoryEntry("LDAP://" & fqdom)
        searcher = New DirectorySearcher()
        searcher.SearchRoot = entry
        searcher.Filter = "(samAccountName=" & usrname & ")"
        searcher.PropertiesToLoad.Add("memberOf")
        result = searcher.FindOne()
        Dim groupCount As Integer = result.Properties("memberOf").Count
        For groupCounter As Integer = 0 To groupCount - 1
            groupCheck = CStr(result.Properties("memberOf")(groupCounter))
            groupCheck = groupCheck.Remove(groupCheck.LastIndexOf(",CN="))
            groupCheck = groupCheck.Replace("CN=", "")
            groupList.Add(groupCheck)
        Next groupCounter

    Catch ex As Exception

    End Try

    Return groupList
End Function


Protected Sub Button1_Click(ByVal sender As Object, _  
   ByVal e As System.EventArgs) Handles Me.Load

    If IsPostBack Then

        Dim name As Boolean = False

        If Not TextBox1.Text = String.Empty Then
            name = True
        End If


        If name = False Then
            StatusLabel.Text = "Update Status: Please Enter Name"

        ElseIf name = True Then


            Group1 = "groupb1"
            Group2 = "groupb2"

            Try
                form1.Visible = False
                Dim groups As New ArrayList()
                groups = GetGroups()
                Dim group As String
                For Each group In groups
                    'NameLabel.Text = group

                    If (group.Contains(Group1)) Then
                        Group1.Text = "User: " & usrname & " is in group1"
                    End If

                    If (group.Contains(Group2)) Then
                        Group1.Text = "User: " & usrname & " is in group2"
                    End If
                Next

                fqdn.Text = "Domain:  " & dom & ".forest.local"
                NameLabel.Text = "User: " & usrname

            Catch ex As Exception

            End Try

        Else
            StatusLabel.Text = "Upload status: Error Please Retry later"
        End If
    End If
End Sub
End Class 

Upvotes: 2

Views: 15855

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460048

Remove the Shared keyword from the method, so replace

Public Shared Function GetGroups() As ArrayList

with

Public Function GetGroups() As ArrayList

You cannot use instance variables like dom from within a Shared method.

You could also make those fields Shared. But that's not a good idea in ASP.NET since it could cause locks and concurrency issues and every request shared the same values(even of different users).

It's also not necessary since you want to use that method from a page method(button-click), so you need an instance of the page anyway.

If you need to persist a value across postback you can use a different way like using ViewState, Session or a HiddenField.

Upvotes: 11

Related Questions