none
Accessing a nested class which is Private RRS feed

  • Question

  • Public Class Container
    
        Public Property var1 As Integer
    
        Private Class Nested
            Public Property var2 As Integer
        End Class
    
        Dim obj_nested As New Nested()
        'When the Nested class is marked as private I can instantiate the Nested Class
        'from within the Container Class as seen in the above statement
        'But I am still unable to access the property "var2"
    
    End Class
    
    
    Module Nested_Example
    
        Sub Main()
    
            Dim obj_container As New Container()
    
            'Dim obj_nested As New Container.Nested()
            'the above statement gives an error when the NESTED class is marked as private
        End Sub
    
    End Module
    

    Questions:

    1. Since the NESTED Class is marked as private I am able to instantiate the NESTED class only inside the Container class. Is there any other way to do it? 

    2. Even after creating the object "Obj_nested" from within the containing class (CONTAINER) I am unable to access the var2 property of the Nested class. How can I access the var2 property? Is it only possible to access this from inside the NESTED class because the class is marked PRIVATE? Asking because, I am under the impression that if am able to create an instance of a PRIVATE class then the through that object I should have access to the field/properties/members of the private class. Otherwise what is the meaning of instantiating? please tell if I am understanding correctly.


    Sougata Ghosh

    Thursday, October 18, 2018 1:09 PM

Answers

  • Sure you can access the variable in the nested class as shown below either in a procedure, function or in a class level variable.

    Module Module1
        Public Class Container
    
            Public Property var1 As Integer
    
            Private Class Nested
                Public Property var2 As Integer
            End Class
    
            Dim obj_nested As New Nested()
            Private example As Integer = obj_nested.var2
            Public Sub Demo()
                obj_nested.var2 = 1
            End Sub
        End Class
        Sub Main()
            Dim obj_container As New Container()
            Console.ReadLine()
        End Sub
    
    End Module


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by sougata12 Thursday, October 18, 2018 1:39 PM
    Thursday, October 18, 2018 1:16 PM
    Moderator

All replies

  • Sure you can access the variable in the nested class as shown below either in a procedure, function or in a class level variable.

    Module Module1
        Public Class Container
    
            Public Property var1 As Integer
    
            Private Class Nested
                Public Property var2 As Integer
            End Class
    
            Dim obj_nested As New Nested()
            Private example As Integer = obj_nested.var2
            Public Sub Demo()
                obj_nested.var2 = 1
            End Sub
        End Class
        Sub Main()
            Dim obj_container As New Container()
            Console.ReadLine()
        End Sub
    
    End Module


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by sougata12 Thursday, October 18, 2018 1:39 PM
    Thursday, October 18, 2018 1:16 PM
    Moderator
  • Hi,

    The above solution works fine. But I have another related question, hence posting here. When an object is created for any class, we can access the properties of the class through that object and direct set it values. For example in the below example car1.Make ="Ford" is setting the value of the Make property for the car1.

    Module Car
    
        Class car
            Public Property make As String
            Public Property miles As Double
        End Class
    
    
        Sub Main()
            Dim car1 As New car()
            car1.make = "Ford"
            car1.miles = 1254.3
        End Sub
    End Module

    But in the nested class example that I have given above I am unable to directly set the "var2" property by direct assignment when accessed through the object "obj_nested". See code below:

    Public Class Container
    
        Public Property var1 As Integer
    
        Private Class Nested
            Public Property var2 As Integer
        End Class
    
        Dim obj_nested As New Nested()
        Private example As Integer = obj_nested.var2 'works fine
    
        Public Sub Demo() ' works fine
            obj_nested.var2 = 1
        End Sub
    
        obj_nested.var2 = 5 'DOES NOT WORK. Red swiggly line coming
    End Class
    
    
    Module Nested_Example
    
        Sub Main()
    
            Dim obj_container As New Container()
            'Cant create object of NESTED here as it is private
        End Sub
    
    End Module

    Question is:
    WHY is the statement obj_nested.var2 =5 not working? Is it not the same as car1.make = "Ford".What am I missing?


    Sougata Ghosh

    Thursday, October 18, 2018 6:02 PM
  • Hi,

    The above solution works fine. But I have another related question, hence posting here. When an object is created for any class, we can access the properties of the class through that object and direct set it values. For example in the below example car1.Make ="Ford" is setting the value of the Make property for the car1.

    Module Car
    
        Class car
            Public Property make As String
            Public Property miles As Double
        End Class
    
    
        Sub Main()
            Dim car1 As New car()
            car1.make = "Ford"
            car1.miles = 1254.3
        End Sub
    End Module

    But in the nested class example that I have given above I am unable to directly set the "var2" property by direct assignment when accessed through the object "obj_nested". See code below:

    Public Class Container
    
        Public Property var1 As Integer
    
        Private Class Nested
            Public Property var2 As Integer
        End Class
    
        Dim obj_nested As New Nested()
        Private example As Integer = obj_nested.var2 'works fine
    
        Public Sub Demo() ' works fine
            obj_nested.var2 = 1
        End Sub
    
        obj_nested.var2 = 5 'DOES NOT WORK. Red swiggly line coming
    End Class
    
    
    Module Nested_Example
    
        Sub Main()
    
            Dim obj_container As New Container()
            'Cant create object of NESTED here as it is private
        End Sub
    
    End Module

    Question is:
    WHY is the statement obj_nested.var2 =5 not working? Is it not the same as car1.make = "Ford".What am I missing?


    Sougata Ghosh

    Please create a new question for this.

    EDIT: Before you do create a new question look very closely at the screenshot below, there are more errors than you think but commenting out one line will fix it as it's totally wrong and can not be used as it is.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, October 18, 2018 11:28 PM
    Moderator