Solved

Public/Shared/Protected variables in VB.NET

Posted on 2006-07-04
6
668 Views
Last Modified: 2006-11-18
I would like to know the difference between
Public/Shared/Protected variables in VB.NET

thanks
0
Comment
Question by:jskfan
  • 3
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17036689
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
 
LVL 8

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 20 total points
ID: 17036967
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
 

Author Comment

by:jskfan
ID: 17050004
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17050070
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
 

Author Comment

by:jskfan
ID: 17051034
I thought when you declare a class as public , other classes can use its methods and variables without declaring them as protected.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 230 total points
ID: 17051100
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now