none
Public class and what can be accessed RRS feed

  • Question

  • Hello all,

    I am writing a small vb.net program and I want to declare some variables out of a function (or should I say method) so that other functions within the class can access them.  But, does this make them available to other programs?

    Public Class Form1

    dim GlobalVariable as integer

    Private Function FOO() ... End Function Private Function BAR() ... End Function

    End Class

    I want both functions FOO and BAR to be able to access GlobalVariable but does this expose GlobalVariable to other programs?  Could another program access GlobalVariable?  If so, is there a way to prevent this without encasing GlobalVariable in a function? 

    Sunday, December 10, 2017 8:03 PM

Answers

  • Hi Frank,

    I actually want to prevent other programs from being able to access the variables.  Some of them will contain sensitive data.  In the unlikely event my computer gets hacked, I don't want the intruder to be able to easily access their contents.

    I am reading up on scope right now.  Would doing something like this work?

    Public Class Form1
    
    Private GlobalVariable as integer
    
    ...

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/private

    I think so if I understand this page correct.


    I must have misread -- I thought that you wanted other programs to have access to it?

    If so then that's automatic - your scope and access level begin and end with your program so no other program has access to them.

    Is that what you meant or did I still miss it?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by nick9995 Sunday, December 10, 2017 8:24 PM
    Sunday, December 10, 2017 8:20 PM

All replies

  • Nick,

    If you want them to be accessible to other applications, reconsider your approach: Create a class library and in each of those other programs, add a reference to that .dll file.

    Then they'll all "see" and use your methods.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, December 10, 2017 8:11 PM
  • Hello,

    See review "Access levels"


    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

    Sunday, December 10, 2017 8:13 PM
    Moderator
  • Hi Frank,

    I actually want to prevent other programs from being able to access the variables.  Some of them will contain sensitive data.  In the unlikely event my computer gets hacked, I don't want the intruder to be able to easily access their contents.

    I am reading up on scope right now.  Would doing something like this work?

    Public Class Form1
    
    Private GlobalVariable as integer
    
    ...

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/private

    I think so if I understand this page correct.


    Sunday, December 10, 2017 8:17 PM
  • Hi Frank,

    I actually want to prevent other programs from being able to access the variables.  Some of them will contain sensitive data.  In the unlikely event my computer gets hacked, I don't want the intruder to be able to easily access their contents.

    I am reading up on scope right now.  Would doing something like this work?

    Public Class Form1
    
    Private GlobalVariable as integer
    
    ...

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/private

    I think so if I understand this page correct.


    I must have misread -- I thought that you wanted other programs to have access to it?

    If so then that's automatic - your scope and access level begin and end with your program so no other program has access to them.

    Is that what you meant or did I still miss it?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by nick9995 Sunday, December 10, 2017 8:24 PM
    Sunday, December 10, 2017 8:20 PM
  • Hi Frank,

    I actually want to prevent other programs from being able to access the variables.  Some of them will contain sensitive data.  In the unlikely event my computer gets hacked, I don't want the intruder to be able to easily access their contents.

    I am reading up on scope right now.  Would doing something like this work?

    Public Class Form1
    
    Private GlobalVariable as integer
    
    ...

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/private

    I think so if I understand this page correct.



    Public Class Form2
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
            ' Initialization here (if applicable)...
    
        End Sub
    End Class
    
    
    
    
    
    Public Class MyMethods
        ' Code here
    End Class

    The class "MyMethods" and the methods/properties/events in that class are accessible to all of your forms' code.

    They are NOT accessible to anything outside of your application though.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, December 10, 2017 8:24 PM
  • Yes that answers my question, thanks!!
    Sunday, December 10, 2017 8:25 PM