none
How to access public variables/constants outside of a singleton class in another class RRS feed

  • Question

  • I have created one singleton class which has the utility function like LogMessage, LoadConstants etc.

    Mu question is I have declared public variables and constants in the class but these variables are not accessible in other class inside the same assembly.

    do i need to use prefix as clsCommon.variablename or is there any other way. I also tried to set the variables as shared but still not accesible from other classes.

    Below is the singleton class code

    Public NotInheritable Class clsCommon
        Private Shared m_sInstance As clsCommon
        Private Shared ReadOnly m_Lock As New Object()


        Public Public Const ConstrStrLeft As String = "FILENET=HC_IT,LOGPATH=C:\LOGFILES\XXX\"
        Public Const ConstrStrMiddle As String = ",LOGGING=YES,SQLSERVER=DBXXX,XSLPATH="
        Public Const ConstrStrRight As String = "COM XSL\"


    Private Sub New()

        End Sub
        Public Shared Function GetInstance() As clsCommon
            SyncLock (m_Lock)
                If m_sInstance Is Nothing Then
                    m_sInstance = New clsCommon
                End If
                Return m_sInstance
            End SyncLock
        End Function


    End Class

    And this is the code form another class from which I am calling the functions from the singleton class.

    Public class AnotherClass

    Dim objCommon As clsCommon = clsCommon.GetInstance()

     Public Sub Load()

      objCommon.LoadConstants(ConstrStrLeft & ConstrStrMiddle & ConstrStrRight)

     End Sub


    End Class

    There is compiler error on this line

    objCommon.LoadConstants(ConstrStrLeft & ConstrStrMiddle & ConstrStrRight)

    which says

    "Name 'ConstrStrLeft' is not declared"

    "Name 'ConstrStrMiddle' is not declared"

    "Name 'ConstrStrRight' is not declared"

     

    Its a little urgent. Thanks in advance.

    Tuesday, August 10, 2010 11:50 AM

Answers

  • Hi,

    indeed you need to prefixe the variables with the class name for Const or Shared variables ( like when you call the GetInstance() method )

    objCommon.LoadConstants(clsCommon.ConstrStrLeft & clsCommon.ConstrStrMiddle & clsCommon.ConstrStrRight)

    Regards

     

     

     


     

    • Marked as answer by SamAgain Wednesday, August 11, 2010 5:58 AM
    Tuesday, August 10, 2010 12:13 PM

All replies

  • Hi,

    indeed you need to prefixe the variables with the class name for Const or Shared variables ( like when you call the GetInstance() method )

    objCommon.LoadConstants(clsCommon.ConstrStrLeft & clsCommon.ConstrStrMiddle & clsCommon.ConstrStrRight)

    Regards

     

     

     


     

    • Marked as answer by SamAgain Wednesday, August 11, 2010 5:58 AM
    Tuesday, August 10, 2010 12:13 PM
  • Thanks for the reply nikho.

    I just wanted to be sure before making the code changes.

     

    Thanks again.

    Wednesday, August 11, 2010 7:04 AM