Solved

Public/Shared/Protected variables in VB.NET

Posted on 2006-07-04
6
666 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

13 Experts available now in Live!

Get 1:1 Help Now