Public/Shared/Protected variables in VB.NET

I would like to know the difference between
Public/Shared/Protected variables in VB.NET

thanks
jskfanAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
If a method is public it can be used by any other object. If it is protected then it can only be accessed by the class it is declared in, or any class that derives from the class in which it is declared, it is not publicly available to any other object unless it exposed by the class in a public property or method.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Public means it is available to everything.
Shared means that the method/field belongs to the class rather than to an instance of the class.
Protected means it is available to the class in which it is declared and any derived classes.
0
 
kaliyugkaarjunConnect With a Mentor Commented:
Hi

just checkout this link
http://www.ondotnet.com/pub/a/dotnet/2003/05/20/introvbnetoo.html?page=1

Also shared variable are private by default.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jskfanAuthor Commented:
let me see if i understand:

1 - if I declare a class / function or procedure/ variable as public, it means it's accessible through the whole projects.

2-if I  declare a function /procedure , variable as shared , it means that I can use them from another class without instanciating them.

3- if I  declare a function /procedure , variable as protected , it means that I can use them from another class but I should instanciating them.

0
 
Carl TawnSystems and Integration DeveloperCommented:
1- Yes.
2 - Yes.
3 - No.

An example:

Public Class Base
    Protected name As String

End Class

Public Class Derived
    Inherits Base

    Public Sub New()
        Me.name = "Bob"       '// Can access name because Derived inherits from Base where name is declared protected
    End Sub
End Class

Public Class Other
    Public Sub New()
        Dim obj As New Base()
        obj.name = "Wilbur"             '// Cannot access "name" in Base because it is protected and we do not inherit from Base
    End Sub
End Class
0
 
jskfanAuthor Commented:
I thought when you declare a class as public , other classes can use its methods and variables without declaring them as protected.
0
All Courses

From novice to tech pro — start learning today.