Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vb.net

I have a question about scope here.

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 need to create a new instance of SampleStructure. I want the function accessible anywhere in my project, but I do not want the structure accessible anywhere, and I don't want it to show up in Intellisense anywhere else.

Is this even possible?

like image 415
cowsay Avatar asked Nov 30 '25 21:11

cowsay


2 Answers

If what you really want to do is just prevent other assemblies from creating instances of SampleStructure you are looking for the access modifier Friend

Change the constructor of SampleStructure to the following

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

If you truly want to make the structure accessible only inside your assembly but still have the function accessible to the outside world, you're out of luck.

like image 56
Peter Karlsson Avatar answered Dec 02 '25 10:12

Peter Karlsson


No, that's not possible. You are returning an instance of the struct so other parts of your program will need to have visibility. How would they be able to interact with it otherwise, or know what type the function returns?

If you were not returning an instance, you could make it private.

like image 31
Kenneth Avatar answered Dec 02 '25 11:12

Kenneth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!