none
SHARED: Declaration context RRS feed

  • Question

  • SHARED can only be sued in a Module level. As per MSDN documentation, Module level means the following:

    within a class, structure, module, or interface but not within a procedure or block

    If that be the case can anyone explain why am I getting an error (see below code) in the statement Public Shared a As Integer

    Module Module1
        Public Shared a As Integer 'Not allowed
    
        Public Class Car
            Public Shared b As Integer 'Allowed
        End Class
    
        Sub Main()
            Shared e As Integer 'Not allowed 
        End Sub
    
    End Module

     I believe it is satisfying all the condition of being within a module but not being within a block or procedure. So why the error?

    Also, I understand from the above code that SHARED cannot be used within Sub Main() because then it violates the condition of not being within a procedure or block. Is my understanding correct?

    Regards,

    Sougata Ghosh


    Sougata Ghosh


    • Edited by sougata12 Thursday, September 19, 2019 10:08 AM
    Thursday, September 19, 2019 9:42 AM

Answers

  • Some here like me are very old programmers. We come from the times when programs were made with almost hardware code. It was Assembler. It exist still however it is only used by small size hardware makers. In the old days we created a map of data, and putted all our data in that. 

    Cobol was invented with that in mind, it had a Data and a Procedure division. 

    Some like that way and still use modules. I don't like it at all, it is very contra productive. But if you want to describe data in a module, you can simply do that in a Module. In a Module data is per definition shared. (Dim X as B is in this case shared).

    Also remember that many make the mistake that OO is the same as OOP. It is not, OOP is programming with objects (types).

    OO is describing data as objects. But that does not mean that the Class is a Type. In the case it is shared it is direct an object.

    With Module Level is meant everything which is not in a member (method or property)


    Success
    Cor




    • Edited by Cor Ligthert Thursday, September 19, 2019 11:18 AM
    • Marked as answer by sougata12 Thursday, September 19, 2019 11:27 AM
    • Unmarked as answer by sougata12 Thursday, September 19, 2019 11:29 AM
    • Marked as answer by sougata12 Thursday, September 19, 2019 11:54 AM
    Thursday, September 19, 2019 11:05 AM
  • Hello,

    Another thought is to use a Singleton pattern.

    Benefits

    • Accessible anywhere in the project
    • You can setup constraints

    In this example, AccountNumber can hold any valid string value while MaxValue is constrained to values under 10.

    Public NotInheritable Class ApplicationData
        Private Sub New()
        End Sub
    
        Public Property AccountNumber() As String
    
        Private _MaxValue As Integer
        Public Property MaxValue() As Integer
            Get
                Return _MaxValue
            End Get
            Set
                If Value > 10 Then
                    Throw New ArgumentOutOfRangeException(
                        "Max value must be less than 10")
                End If
                _MaxValue = Value
            End Set
        End Property
    
        Private Shared ReadOnly Lazy As New Lazy(Of ApplicationData)(
            Function()
                Return New ApplicationData()
            End Function)
    
        Public Shared ReadOnly Property Instance() As ApplicationData
            Get
                Return Lazy.Value
            End Get
        End Property
    End Class

    The following code might be in a code module, class or form.

    ApplicationData.Instance.MaxValue = 9
    ApplicationData.Instance.AccountNumber = "B123"

    Then the following perhaps is in a different class, code module or form.

    MessageBox.Show($"Max value: {ApplicationData.Instance.MaxValue}")
    ApplicationData.Instance.MaxValue = 11
    The last line would raise an exception as it's higher than 10.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by sougata12 Thursday, September 19, 2019 3:22 PM
    Thursday, September 19, 2019 12:09 PM
    Moderator

All replies

  • Saugate,

    With VB Net you can use OOP or classic programming. 

    OOP is based on creating objects from classes which can be reused. 

    In past you allocated a part of the memory and that was it. It never was removed, you had always to clean every singly byte of it, if you started with a new entity. (Some think that reusing of the same part is less memory expensive, they forget that code takes much more bytes than data and processing time than just setting a new reference to a new instanced piece of memory and remove the reference of the old one and let the memory part be cleaned by the garbage collector).

    A Module in VB is to be used as fixed (static) memory. (Be aware it is not really fixed as in history, the Windows OS swaps it many times). 

    In past in 2002 they probably wanted to get rid of the Module keyword, it looks so antic VB. They opened a way likewise in C++, Java and C#.

    However, the keyword in those languages "Static" was already used for another purpose in VB and gave it the name "Shared". "Static" can be used on method level, to make a field fixed in memory but used in a method. (In practise it creates a field on class level). 

    In version 2005 they stopped with that idea and kept the Module in the same way as it was from the beginning, but kept the shared way available most probably to avoid breaking chances. 

    Be aware there are differences between the use of the keyword "Static" in C# and "Shared" in VB. 

    In C# it has more possibilities to align with a classic way of C++ programming, but that is as well not done for OOP. 

    Module Module1
        Public a As Integer 'is Static/Shared
        Sub Main()
            Dim e As Integer  
            Dim z As Integer = Car.b
        End Sub
        Public Class Car
            Public Shared b As Integer 'is Static/Shared 
            Public Sub Whatever()
                Static c As Integer 'Is Static, but Not shared
            End Sub
        End Class
    End Module


    Success
    Cor








    • Edited by Cor Ligthert Thursday, September 19, 2019 10:36 AM
    Thursday, September 19, 2019 10:12 AM
  • Hi,

    To be honest I did not fully understand your point. In your example you have mentioned that the statement Public a as Integer is both static and shared. What I understand is that anything within the Module 1 can access the variable 'a" and hence there is no need to use the SHARED keyword. Is that what you mean?

    You also mention that a module in VB is to be treated a a fixed memory. I did not understand its implication on the definition of Module Level. Could you please explain that?



    Sougata Ghosh


    • Edited by sougata12 Thursday, September 19, 2019 11:25 AM
    Thursday, September 19, 2019 10:43 AM
  • Some here like me are very old programmers. We come from the times when programs were made with almost hardware code. It was Assembler. It exist still however it is only used by small size hardware makers. In the old days we created a map of data, and putted all our data in that. 

    Cobol was invented with that in mind, it had a Data and a Procedure division. 

    Some like that way and still use modules. I don't like it at all, it is very contra productive. But if you want to describe data in a module, you can simply do that in a Module. In a Module data is per definition shared. (Dim X as B is in this case shared).

    Also remember that many make the mistake that OO is the same as OOP. It is not, OOP is programming with objects (types).

    OO is describing data as objects. But that does not mean that the Class is a Type. In the case it is shared it is direct an object.

    With Module Level is meant everything which is not in a member (method or property)


    Success
    Cor




    • Edited by Cor Ligthert Thursday, September 19, 2019 11:18 AM
    • Marked as answer by sougata12 Thursday, September 19, 2019 11:27 AM
    • Unmarked as answer by sougata12 Thursday, September 19, 2019 11:29 AM
    • Marked as answer by sougata12 Thursday, September 19, 2019 11:54 AM
    Thursday, September 19, 2019 11:05 AM
  • Thanks....Just to be very sure...

    What I understand is that anything within the Module 1 can access the variable 'a" and hence there is no need to use the SHARED keyword. It is by default shared. Is that what you mean?

    I have unmarked the answer to keep the thread open to draw your attention. I shall it close the thread once you reply to this. 



    Sougata Ghosh


    • Edited by sougata12 Thursday, September 19, 2019 11:30 AM
    Thursday, September 19, 2019 11:29 AM
  • Yes

    Success
    Cor


    • Edited by Cor Ligthert Thursday, September 19, 2019 11:43 AM
    Thursday, September 19, 2019 11:41 AM
  • Hello,

    Another thought is to use a Singleton pattern.

    Benefits

    • Accessible anywhere in the project
    • You can setup constraints

    In this example, AccountNumber can hold any valid string value while MaxValue is constrained to values under 10.

    Public NotInheritable Class ApplicationData
        Private Sub New()
        End Sub
    
        Public Property AccountNumber() As String
    
        Private _MaxValue As Integer
        Public Property MaxValue() As Integer
            Get
                Return _MaxValue
            End Get
            Set
                If Value > 10 Then
                    Throw New ArgumentOutOfRangeException(
                        "Max value must be less than 10")
                End If
                _MaxValue = Value
            End Set
        End Property
    
        Private Shared ReadOnly Lazy As New Lazy(Of ApplicationData)(
            Function()
                Return New ApplicationData()
            End Function)
    
        Public Shared ReadOnly Property Instance() As ApplicationData
            Get
                Return Lazy.Value
            End Get
        End Property
    End Class

    The following code might be in a code module, class or form.

    ApplicationData.Instance.MaxValue = 9
    ApplicationData.Instance.AccountNumber = "B123"

    Then the following perhaps is in a different class, code module or form.

    MessageBox.Show($"Max value: {ApplicationData.Instance.MaxValue}")
    ApplicationData.Instance.MaxValue = 11
    The last line would raise an exception as it's higher than 10.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by sougata12 Thursday, September 19, 2019 3:22 PM
    Thursday, September 19, 2019 12:09 PM
    Moderator
  • Thanks Karen for the alternate view.

    Sougata Ghosh

    Thursday, September 19, 2019 3:23 PM
  • Thanks Karen for the alternate view.

    Sougata Ghosh

    Your welcome. My thought is one should have more than one option available is both have merit :-)

    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 19, 2019 3:24 PM
    Moderator