|
-
Mar 22nd, 2006, 06:51 AM
#1
Thread Starter
Frenzied Member
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________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
-
Mar 22nd, 2006, 06:55 AM
#2
Frenzied Member
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
-
Mar 22nd, 2006, 07:02 AM
#3
Re: friend functions in vb??
 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.
 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?
-
Mar 22nd, 2006, 07:04 AM
#4
Frenzied Member
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
-
Mar 22nd, 2006, 07:06 AM
#5
Thread Starter
Frenzied Member
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________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
-
Mar 22nd, 2006, 07:09 AM
#6
Frenzied Member
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
-
Mar 22nd, 2006, 07:21 AM
#7
Re: friend functions in vb??
 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.
 Originally Posted by yrwyddfa
What purpose does that serve?
Beats the heck out of me. Maybe someone else knows.
-
Mar 22nd, 2006, 07:45 AM
#8
Thread Starter
Frenzied Member
Re: friend functions in vb??
and what else does vb have ? operator overloading ,inheritance ? would be really interesting if it does have
__________________
________________0îîî___
___îîî0________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
-
Mar 22nd, 2006, 01:35 PM
#9
Fanatic Member
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.
-
Mar 22nd, 2006, 02:53 PM
#10
Re: friend functions in vb??
Example: If you have an UDT declared in a module like this..
VB Code:
Public Type myUDT
var1 As String
var2 As Integer
End Type
And you want to use it as a returned value from a Public Function in a Class..
VB Code:
Public Function getUDT() As myUDT
Dim s As myUDT
s.var1 = "hi"
s.var2 = 1
getUDT = s
End Function
Then, when you try to call that function from where you instanciated your object.. (example, from a Form)
VB Code:
Private Sub Command1_Click()
Dim typ As myUDT
Dim MyObj As Class1
Set MyObj = New Class1
typ = MyObj.getUDT
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:
Friend Function getUDT() As myUDT
Dim s As myUDT
s.var1 = "hi"
s.var2 = 1
getUDT = s
End Function
That way it knows myUDT is declared public in a module, and picks up its declaration succesfully from there.
-
Mar 23rd, 2006, 09:04 AM
#11
Thread Starter
Frenzied Member
Re: friend functions in vb??
that clears my all my doubts
thanks jcis
__________________
________________0îîî___
___îîî0________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
-
Mar 23rd, 2006, 09:12 AM
#12
Re: friend functions in vb??
 Originally Posted by litlewiki
and what else does vb have ? operator overloading ,inheritance ? would be really interesting if it does have
No and no
-
Mar 23rd, 2006, 10:47 AM
#13
Re: friend functions in vb??
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|