If I have a function inside a module that returns a custom structure, can I make this structure inaccessible outside the module?

I have a question about the scope here.

Let's say I have the following module:

Public Module SampleModule

    Public Function SampleFunction() As SampleStructure
        Return New SampleStructure(123, 456)
    End Function

    Structure SampleStructure 'Do not want this accessible elsewhere in project
        Public A As Integer
        Public B As Integer
        Sub New(ByVal A As Integer, ByVal B As Integer)
            Me.A = A
            Me.B = B
        End Sub
    End Structure

End Module

The function SampleFuncton()is the only code in the entire project that will ever be needed to create a new instance SampleStructure. I want the function to be available anywhere in my project, but I do not want the structure to be accessible anywhere, and I do not want it to appear in Intellisense anywhere else.

Is it possible?

+3
source share
3 answers

, , SampleStructure, Friend

SampleStructure

Friend Sub New(ByVal A As Integer, ByVal B As Integer)
    Me.A = A
    Me.B = B
End Sub

, , , , .

+3

, . , . , ?

, .

+1

. .

1) Create an interface that exposes all the properties, methods, functions, etc. that you would like available.

Public Interface Sample
    ReadOnly Property A() As Integer
    ReadOnly Property B() As Integer
End Interface

2) Create a private structure and implement the interface Sample.

Private Structure InternalSample
    Implements Sample
    Friend Sub New(ByVal A As Integer, ByVal B As Integer)
        Me.m_a = A
        Me.m_b = B
    End Sub
    Public ReadOnly Property A() As Integer Implements Sample.A
        Get
            Return Me.m_a
        End Get
    End Property
    Public ReadOnly Property B() As Integer Implements Sample.B
        Get
            Return Me.m_a
        End Get
    End Property
    Friend m_a As Integer
    Friend m_b As Integer
End Structure

3) In the function, GetSamplecreate a new instance InternalSample, set the desired values ​​and return the object.

Public Function GetSample() As Sample
    Dim struct As New InternalSample(123, 456)
    'You can still change the values before returning the object:
    struct.m_a = 321
    struct.m_b = 654
    Return struct
End Function
0
source

All Articles