Public/Shared/Protected variables in VB.NET

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

Question by:jskfan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
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.

Assisted Solution

kaliyugkaarjun earned 80 total points
ID: 17036967

just checkout this link

Also shared variable are private by default.

Author Comment

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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Author Comment

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

Accepted Solution

Carl Tawn earned 920 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.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

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