Search code examples
vb.netshared

How can i access shared members in a another class


I have the following public class with shared members:

Public Module Modul1

Sub Main()
    Rectangle.Height = 3
End Sub

Public Class Rectangle
    Public Shared Length As Double
    Public Shared Height As Double

    Public Shared Function CalculatePerimeter#()
        Return (Length + Height) * 2
    End Function

    Public Shared Function CalcaulteArea#()
        Return Length * Height
    End Function
End Class
End Module

now i added a new class in vs (new tab) and tried to access the class Rectange

Public Class test
Rectangle.
End Class

But intelisence gives me nothing. Why can i not access the class? It is public and the members are shared.


Solution

  • Because you are in a region where you can declare fields or properties or method bodies. Here you cannot access methods, properties or fields of other classes.

    This would work:

    Public Class test
        ' Rectangle.He...  <-- does not work because you can only use accessors or Dim etc.
        Dim height As Double = Rectangle.Height
    End Class
    

    Why should intellisense give you options that won't compile anyway?