Problem getting information from non-shared object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MFSPZ
    New Member
    • Mar 2011
    • 3

    #1

    Problem getting information from non-shared object

    I get an error when I declare an object NOT-shared in a subclass.
    I can not make this object (glNR) shared.
    Is there an other way?

    Code...
    Code:
    Public Class clsRap
      Private glNr As String = ""  [I]' not shared[/I]
    
      Public Class clsHd
        Public Sub Hd(ByVal _D As SPZClasses.clsPDF)
           [B]dim xx = glNr[/B]  [I]' Here I get the error[/I]
           ' I need the information set in RPT1 !!
        End Sub
      End Class
     
      Public Function RPT1(ByVal _ID As String)
        Dim resultaat As Boolean = True
        [B]glNr = _ID[/B]
        Return resultaat
      End Function
    End Class  'clsRap
    
    Public Class Rap1
      Dim Doc as new clsRap
      Doc.RPT1(123)
    End Class
    The error is 'Reference to a non-shared member requires an object reference'.

    Does anyone can help ?
    Thanks.
    Last edited by Niheel; Mar 9 '11, 07:44 PM.
  • R MacDonald
    New Member
    • Mar 2011
    • 24

    #2
    Hello, MFSPZ,

    You will need to pass a reference to the "parent class" object into the "nested class" function. See the modified version of your example below:

    Code:
    Public Class clsRap
        Private glNr As String = "" ' not shared
    
        Public Class clsHd
            Public Sub Hd(ByVal _D As Object, ByVal ParentClass As clsRap)
                Dim xx As String = ParentClass.glNr ' Here I get the error
                ' I need the information set in RPT1 !!
            End Sub
        End Class
    
        Public Function RPT1(ByVal _ID As String) As Boolean
            Dim resultaat As Boolean = True
            glNr = _ID
            Return resultaat
        End Function
    End Class 'clsRap
    Last edited by Niheel; Mar 9 '11, 07:44 PM.

    Comment

    • MFSPZ
      New Member
      • Mar 2011
      • 3

      #3
      Thanks.
      But how can I get the parentClass? I should know this in function RPT1.

      Comment

      • R MacDonald
        New Member
        • Mar 2011
        • 24

        #4
        Hello, MFSPZ,

        RPT1 is a function of the parent class. See the sample below for an example of calling Hd from within RPT1. Since your sample Hd function doesn't actually do anything, I have added a Debug.PrintLine statement to it to illustrate the result. I have also appended Button1_Click code to demonstrate how the Hd function can be invoked, first from within the RPT1 method, and then directly. Please see below.

        Cheers,
        Randy

        Hello, MFSPZ,

        RPT1 is a function of the parent class. See the sample below for an example of calling Hd from within RPT1. Since your sample Hd function doesn't actually do anything, I have added a Debug.PrintLine statement to it to illustrate the result. I have also appended Button1_Click code to demonstrate how the Hd function can be invoked, first from within the RPT1 method, and then directly. Please see below.

        Cheers,
        Randy

        Public Class clsRap
        Private glNr As String = "" ' not shared

        Public Class clsHd
        ''Public Sub Hd(ByVal _D As Object)
        '' Dim xx As String = glNr ' Here I get the error
        '' ' I need the information set in RPT1 !!
        ''End Sub
        Public Sub Hd(ByVal _D As Object, ByVal ParentClass As clsRap)
        Dim xx As String = ParentClass.glN r
        ' I need the information set in RPT1 !!
        Debug.WriteLine ("The value of xx is """ & xx & """.")
        End Sub
        End Class

        Public Function RPT1(ByVal _ID As String) As Boolean
        Dim resultaat As Boolean = True
        glNr = _ID

        ' Example showing a call to the nested class function
        ' from inside the parent class function RPT1.
        Dim NestedHdClass As New clsHd
        NestedHdClass.H d(Nothing, Me)

        Return resultaat
        End Function

        End Class 'clsRap

        Public Class Rap1
        Dim Doc As New clsRap

        Sub Testit()
        Debug.WriteLine ("Value = " & Doc.RPT1("123") )
        End Sub
        End Class


        Public Class Form1

        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

        ' Example showing a call to the nested class function
        ' from inside the parent class function RPT1.
        Dim SampleParentCla ss As New clsRap
        SampleParentCla ss.RPT1("Set the glNr value to be this string.")

        ' Example showing a call to the nested class function
        ' from outside of the parent class.
        Dim SampleNestedCla ss As New clsRap.clsHd
        SampleNestedCla ss.Hd(Nothing, SampleParentCla ss)

        End Sub

        End Class

        Comment

        • R MacDonald
          New Member
          • Mar 2011
          • 24

          #5
          Hi, MFSPZ,

          I'm sorry about the lack of formatting in the code in the above sample. It seems that all formatting gets stripped away when I post to the thread. Perhaps there is a way to avoid this. But I'm a newbie here and if there is a way I haven't yet learned how.

          Cheers,
          Randy

          Comment

          • MFSPZ
            New Member
            • Mar 2011
            • 3

            #6
            Thanks Randy !
            I have solved the problem.

            Comment

            Working...