|
-
Jun 22nd, 2006, 02:33 AM
#1
Thread Starter
Frenzied Member
Understand a Friend
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
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Jun 22nd, 2006, 02:41 AM
#2
Re: Understand a Friend
Access Levels in Visual Basic
Friend behaves like Public within the project it was declared in and like Private outside that project.
-
Jun 22nd, 2006, 02:50 AM
#3
Thread Starter
Frenzied Member
Re: Understand a Friend
Thank You for the quick explanation.
I am having trouble going to the url that you provided.
Also, there is Protected and Protected Friend.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Jun 22nd, 2006, 02:58 AM
#4
Re: Understand a 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.
-
Jun 22nd, 2006, 03:07 AM
#5
Thread Starter
Frenzied Member
Re: Understand a Friend
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
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Jun 22nd, 2006, 03:23 AM
#6
Re: Understand a Friend
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
-
Jun 22nd, 2006, 04:02 AM
#7
Thread Starter
Frenzied Member
Re: Understand a Friend
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?
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Jun 22nd, 2006, 04:14 AM
#8
Re: Understand a Friend
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|