Results 1 to 13 of 13

Thread: friend functions in vb??

  1. #1

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Unhappy friend functions in vb??

    Friend functions are functions defined outside a class (ie, not member functions), but which the class declares to be friends so that they can use the class's private members.

    this is i guess the basic definition of a friend function but i have seen codes where friend keyword is used before functions in vb6.as far as i know vb6 is an event driven language and not an OOB language.then how are friend functions implemented ??
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  2. #2
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: friend functions in vb??

    A Friend function is always a function in a class. It's concept is about scope.

    A friend function is visible to any callee inside the project of where it exists; it cannot be seen, nor called, outside the compilation unit (ActiveX DLL for instance)
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: friend functions in vb??

    Quote Originally Posted by yrwyddfa
    A Friend function is always a function in a class.
    According to the VB Help file, it can be used in a form module as well.
    Quote Originally Posted by VB Help File
    Modifies the definition of a procedure in a form module or class module to make the procedure callable from modules that are outside the class, but part of the project within which the class is defined. Friend procedures cannot be used in standard modules.
    Friend is used when you want a property or method to be available to other objects in your project but not to the project as a whole. Does that make sense?

  4. #4
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: friend functions in vb??

    I didn't realise they could be in modules? What purpose does that serve? A public function in a module is only visible inside the project anyway, so a friend function is pretty much the same thing.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  5. #5

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: friend functions in vb??

    hmmm... interesting so does that mean if a designate a sub as a protected one even other subs in the same module will not be able to access it ??

    does this make sense?

    p.s i understood the concept of friend functions ,thanks hack and vrwy.... !!
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  6. #6
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: friend functions in vb??

    VB6 does not have the concept of 'protected' but .Net does (I believe) . . . .
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: friend functions in vb??

    Quote Originally Posted by yrwyddfa
    I didn't realise they could be in modules?
    Actually, until I looked Friend up in the Help file I didn't either.
    Quote Originally Posted by yrwyddfa
    What purpose does that serve?
    Beats the heck out of me. Maybe someone else knows.

  8. #8

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: friend functions in vb??

    and what else does vb have ? operator overloading ,inheritance ? would be really interesting if it does have
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  9. #9
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: friend functions in vb??

    source:
    http://www.pgacon.com/visualbasic.ht...iend%20Keyword


    When writing property procedures and methods for a VB class there are three keywords you can use to control access to the property or method. Two of these are straightforward and easy to understand:

    Public. There are no restrictions on access.
    Private. The property or method is available only from within its own class.
    The third keyword is Friend, and its use and meaning are not well understood. You use Friend when you want a property or method to be accessible to other objects in the project but not to the project as a whole. Or, within a component, objects can access each other's Friend members but the program that is using the component cannot.

    Suppose you are writing a component as a DLL or ActiveX, and there are several objects in the component that need to communicate with each other. However you do not want this communication channel to be available to the program that is calling the component. This is perhaps the most common use of Friend properties and methods.

    Friend properties and methods are not part of a class's interface. They do not appear in the type library of the class and they are not included when you implement an interface using the Implements keyword.

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: friend functions in vb??

    Example: If you have an UDT declared in a module like this..
    VB Code:
    1. Public Type myUDT
    2.     var1 As String
    3.     var2 As Integer
    4. End Type
    And you want to use it as a returned value from a Public Function in a Class..
    VB Code:
    1. Public Function getUDT() As myUDT
    2.     Dim s As myUDT
    3.     s.var1 = "hi"
    4.     s.var2 = 1
    5.     getUDT = s
    6. End Function
    Then, when you try to call that function from where you instanciated your object.. (example, from a Form)
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim typ As myUDT
    3. Dim MyObj As Class1
    4.  
    5.     Set MyObj = New Class1
    6.     typ = MyObj.getUDT
    7. End Sub
    With that you see this..
    Code:
    Compile error:
     Only public user defined types defined in public object modules can be used
     as parameters or return types for public procedures of class modules or as
     fields of public user defined types
    To fix it, you need to declare Function getUDT in the class, as Friend:
    VB Code:
    1. Friend Function getUDT() As myUDT
    2.     Dim s As myUDT
    3.     s.var1 = "hi"
    4.     s.var2 = 1
    5.     getUDT = s
    6. End Function
    That way it knows myUDT is declared public in a module, and picks up its declaration succesfully from there.

  11. #11

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: friend functions in vb??

    that clears my all my doubts
    thanks jcis
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: friend functions in vb??

    Quote Originally Posted by litlewiki
    and what else does vb have ? operator overloading ,inheritance ? would be really interesting if it does have
    No and no

  13. #13
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: friend functions in vb??

    Quote Originally Posted by Hack
    Actually, until I looked Friend up in the Help file I didn't either.Beats the heck out of me. Maybe someone else knows.
    Friend means you can access it from within your own application only. Anyone else creating a copy of your objects outside (createobject for example) can only access those methods you called Public.

    PS: Ive only tried it in .Net.


    --

    @litlewiki and what else does vb have ? operator overloading ,inheritance ? would be really interesting if it does have

    Get vb.net !!

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