Results 1 to 8 of 8

Thread: Understand a Friend

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class Class1
    2.     Public var1 As Integer
    3.     Protected var2 As Integer
    4.     Private var3 As Integer
    5. End Class
    6.  
    7. Public Class Class2
    8.     Inherits Class1
    9.  
    10.     Public Sub Sub1()
    11.         Me.var1 = 1 '<- OK.  Public member variable inherited from Class1
    12.         Me.var2 = 2 '<- OK.  Protected member variable inherited from Class1
    13.         Me.var3 = 3 '<- Not allowed.  Private member not inherited from Class1
    14.     End Sub
    15.  
    16.     Public Sub Sub2()
    17.         Dim c1 As New Class1
    18.  
    19.         c1.var1 = 1 '<- OK.  Public member accessible from outside Class1
    20.         c1.var2 = 2 '<- Not allowed.  Protected member not accessible from outside Class1
    21.         c1.var3 = 3 '<- Not allowed.  Private member not accessible from outside Class1
    22.     End Sub
    23. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width