Friend Class Help

mjs7231
07-30-2004, 08:45 AM
I am trying to write a class and to get it to compile I had to declaire one of the functions as a freind. However, I cannot seem to call this function from another, the error simply says I this method is not supported. Here is a sample of my code: Any ideas why I am getting this?

'This is cls_PulsePK
Public myPulse As Object
Public Type Pulse_Channel
Enabled As Boolean
title As String
Type As String
End Type

Public Sub Main()
Set myPulse = New clsPulse
End Sub

'I GET THE ERROR ON THIS FUNCTION WHEN TRYING TO CALL THE FUNCTION GETBK_CONFIGCHANNELNAMES.
Public Function PK_GetNumMics() As Integer
Dim Channels() As Pulse_Channel
Channels = myPulse.GetBK_ConfigChannelNames
End Sub


'This is clsPulse
Private BK_ConfigChannelNames() As Pulse_Channel

Friend Function GetBK_ConfigChannelNames() As Pulse_Channel()
GetBK_ConfigChannelNames = BK_ConfigChannelNames
End Function

dipique
07-30-2004, 09:02 AM
I am trying to write a class and to get it to compile I had to declaire one of the functions as a freind. However, I cannot seem to call this function from another, the error simply says I this mothod is not supported. Here is a sample of my code: Any ideas why I am getting this?

'This is cls_PulsePK
Public myPulse As Object
Public Type Pulse_Channel
Enabled As Boolean
title As String
Type As String
End Type

Public Sub Main()
Set myPulse = New clsPulse
End Sub

'I GET THE ERROR ON THIS FUNCTION WHEN TRYING TO CALL THE FUNCTION GETBK_CONFIGCHANNELNAMES.
Public Function PK_GetNumMics() As Integer
Dim Channels() As Pulse_Channel
Channels = myPulse.GetBK_ConfigChannelNames
End Sub


'This is clsPulse
Private BK_ConfigChannelNames() As Pulse_Channel

Friend Function GetBK_ConfigChannelNames() As Pulse_Channel()
GetBK_ConfigChannelNames = BK_ConfigChannelNames
End Function

Have you tried declaring it as public instead of friend?

Dan

mjs7231
07-30-2004, 09:10 AM
Have you tried declaring it as public instead of friend?

Dan

If I declare it as public the program unfortunatly wont even compile.

noi_max
07-30-2004, 09:15 AM
This had me wondering. I tried to call a friend function in one class module from another in the same manner and was a-ok.

one line that troubles me

Public myPulse As Object

why not declare it explicitly

Public myPulse As clsPulse


Also the Channels() array. I don't see it getting dimensioned? :confused:

mjs7231
07-30-2004, 09:23 AM
This had me wondering. I tried to call a friend function in one class module from another in the same manner and was a-ok.

one line that troubles me

Public myPulse As Object

why not declare it explicitly

Public myPulse As clsPulse


I am not calling it explicitly because I also wrote a tstPulse Class that will simulate the clsPulse class if I do not have the 3rd party software I am running. (confusing i know)



Also the Channels() array. I don't see it getting dimensioned? :confused:

The channels array is not dimensioned because It is supposed to grab the same channel array type from clsPulse (or tstPulse). The array has a dimension in that class and will be returned. This means that when the array is passed from GetBK_ConfigChannelNames, it will be dimensioned.

OnErr0r
07-30-2004, 09:24 AM
Is this an EXE or AX Dll project? And specify the VB version.

mjs7231
07-30-2004, 09:25 AM
Is this an EXE or AX Dll project? And specify the VB version.

I am using VB6 and creating an EXE.

OnErr0r
07-30-2004, 09:30 AM
You can only have a public type in a public object module (dll). Move your public type to a BAS module.

OnErr0r
07-30-2004, 09:36 AM
Also, As Object is probably not going to work. But As clsPulse should.

mjs7231
07-30-2004, 09:38 AM
You can only have a public type in a public object module (dll). Move your public type to a BAS module.

The type is declared in a BAS module. I am sorry I should have specified that clsPulse_PK (the first bit of code) is a module. clsPulse (the second bit) of code is a class.

This is a ext meaning of what I am trying to do from my original post.
---------------------------------------------------------------------
1.) MODULE clsPulse_PK:
I declare my public type Pulse_Channels
I also declare a variable (lets call it A) of this type.

2.) CLASS clsPulse:
I create another variable (lets call it B) of the public type in clsPulse_PK and assign a value to it.

3.) BACK AT MODULE clsPulse_PK:
I try to call a function in clsPulse that will return the varable B. However I get an error that states "Method does not support this method or class).

mjs7231
07-30-2004, 09:45 AM
Also, As Object is probably not going to work. But As clsPulse should.

I tried declaring myPulse as you stated and it works. I suppose that I don't need to change which class myPulse refers to while the program is running, since the other refrence it will refer to (tstPulse) is only for testing purposes.

So as of right now things work! THANKS!! Is there an explination of why what I was trying to do wouldn't work?

OnErr0r
07-30-2004, 09:47 AM
np. :)

dipique
07-30-2004, 10:19 AM
np. :)

I guess that's a "no" to your question! :) As far as I understand it, the only reason it didn't work is that certain declarations are allowed in certain types of modules, and you were trying to use a declaration that wasn't allowed. For a more technical explanation, you may have to resort to the MSDN mire.

Dan

OnErr0r
07-30-2004, 09:04 PM
I guess that's a "no" to your question! :) As far as I understand it, the only reason it didn't work is that certain declarations are allowed in certain types of modules, and you were trying to use a declaration that wasn't allowed. For a more technical explanation, you may have to resort to the MSDN mire.

Dan

Actually, "np" is short for no problem. I did not notice the question mjs7231 asked.

VB uses the IUnknown interface to access public class methods with a COM vtable when As ClassName (early bound) is used. Friend and Private members are similar in that a lookup table to the functions is used and they are accessible very quickly. Private functions are not exposed outside the class. Friend functions are a special case, they are accessible outside of a class. When you use As Object (late bound) the IDispatch interface is used. It can lookup methods not defined at runtime, but with a substantial speed penalty. IDispatch has no way to lookup a friend function which is not part of the class vtable. This is why As Object cannot find the friend method in your class.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum