I just converted one of my VB6 project using the 2005 Wizard.
I understand the concept of Public and Private but what is Friend? Also, is there anything else in the same line as this?
Thank You
Printable View
I just converted one of my VB6 project using the 2005 Wizard.
I understand the concept of Public and Private but what is Friend? Also, is there anything else in the same line as this?
Thank You
Access Levels in Visual Basic
Friend behaves like Public within the project it was declared in and like Private outside that project.
Thank You for the quick explanation.
I am having trouble going to the url that you provided.
Also, there is Protected and Protected Friend.
MSDN seems to be labouring at the moment. It loaded really slowly for me too. Maybe they're having server issues at the moment. Give it time. Protected Friend is like Public within the project it was declared in (that's the Friend part) and like Protected outside that project. It is the least commonly used access level.
Thank You but I actually found this too.
http://msdn.microsoft.com/library/de...vastmclass.asp
However, still a bit confuse on Protected.
Also, I should be using Friend if I know that another program is not going to access this program right?
Thank You
You should pretty much declare all your types as Friend in an application. If you declare the type itself as Friend then you can leave all the members as Public and they are still inaccessible outside the assembly. With library projects its a bit both ways. If you intend for the applications that reference your library to use a type then declare it as Public, but if it is only supposed to be used within your library by your own types then declare it as Friend. The C# equivalent of Friend is "internal", which probably is a bit more descriptive.
Here's an example of the difference between Public, Protected and Private:VB Code:
Public Class Class1 Public var1 As Integer Protected var2 As Integer Private var3 As Integer End Class Public Class Class2 Inherits Class1 Public Sub Sub1() Me.var1 = 1 '<- OK. Public member variable inherited from Class1 Me.var2 = 2 '<- OK. Protected member variable inherited from Class1 Me.var3 = 3 '<- Not allowed. Private member not inherited from Class1 End Sub Public Sub Sub2() Dim c1 As New Class1 c1.var1 = 1 '<- OK. Public member accessible from outside Class1 c1.var2 = 2 '<- Not allowed. Protected member not accessible from outside Class1 c1.var3 = 3 '<- Not allowed. Private member not accessible from outside Class1 End Sub End Class
Thanks for taking out the time to create the example.
OK, so this is what I tried to make sense out from your example and if I am wrong, forgive me.
Public - Anything can access it.
Private - Only on a Module Level can access it.
Protected - It is in a way like Private but can only be access from inheritance.
If the above is correct, when would I use Protected?
One of the most common uses of Protected is on the methods that raise events within a class. For instance, the TextChanged event of the Control class is raised by the OnTextChanged method, which is declared Protected. You don't want just anybody to be able to raise your TextChanged event, so you don't want the OnTextChanged method accessible from outside the class. If you create your own control though, you'll need to be able to raise its TextChanged event, so you do want the OnTextChanged method accessible from within classes that inherit Control. That's exactly how Protected works.