0

I have this class:

''' <summary>
''' Utility class for managing ASP.NET session state.
''' </summary>
Public Class SessionHelper
    ''' <summary>
    ''' Private constructor to prevent instantiation since classes cannot be declared Shared in VB.
    ''' </summary>
    Private Sub New()
    End Sub

    ''' <summary>
    ''' Retrieves the current session state.
    ''' </summary>
    ''' <returns></returns>
    Public Shared ReadOnly Property Session As HttpSessionState
        Get
            Return HttpContext.Current.Session
        End Get
    End Property

    ''' <summary>
    ''' Retrieves the current system connection string.
    ''' </summary>
    ''' <returns></returns>
    Public Shared ReadOnly Property ConnectionString As String
        Get
            Dim raw As String = Session(SessionConstants.DotNetConnectionString)
            If raw Is Nothing Then Return Nothing
            Return raw.Replace(",", ";")
        End Get
    End Property

    ''' <summary>
    ''' Gets or sets a session variable.
    ''' </summary>
    ''' <param name="key">The key of the session variable.</param>
    ''' <returns>The value of the session variable.</returns>
    Public Shared Property Item(ByVal key As String) As Object
        Get
            Return Session(key)
        End Get
        Set(value As Object)
            Session(key) = value
        End Set
    End Property
End Class

And I get a compile error when I try to call the shared indexer:

SessionHelper("DotNetConnectionString")

Error BC30109 'SessionHelper' is a class type and cannot be used as an expression.

What am I doing wrong? Or does VB just not support shared indexers? I can call it explicitly as SessionHelper.Item("DotNetConnectionString") but the indexer syntax is not working.

ekolis
  • 6,270
  • 12
  • 50
  • 101

1 Answers1

1

You are accessing the method like a default indexer by leaving out the "Item" method name, but the method is not declared 'Default'. In fact, you can't declare shared indexers in VB.

e.g., you have to change your call to the shared method:

SessionHelper.Item("DotNetConnectionString")

Note: I noticed after posting that Ahmed had included the same information in a comment, so he deserves full credit.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28