Solved

User defined types returned from a class

Posted on 2002-04-03
14
232 Views
Last Modified: 2010-05-02
I have this type in a module:

---------------------------------------------------
Public Type PhoneFlags
    PREF As Boolean
    WORK As Boolean
    HOME As Boolean
End Type

Public Type PhoneNumberType
    PhoneNumberTypes As PhoneFlags
    Number As String
End Type
---------------------------------------------------

and in a Class I have this:

Public Property Get PhoneType() As PhoneFlags
    PhoneType = Phone.PhoneNumberTypes
End Property


But, alas, the compiler tells me that "Only public user defined types defined in public object modules can be used as parameters or return types for public procedures of class modules..."

I thought declaring PhoneFlags as Public in the main module would make in usable by the class, but that is apparently not the case.  Can someone tell me the error of my ways please?
0
Comment
Question by:jbunting
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6915771
You need to put it in your class, so that users of your class can see it.

Zaphod.
0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6915825
How about

Friend Property Get PhoneType() As PhoneFlags
   PhoneType = Phone.PhoneNumberTypes
End Property

Friend is seen inside Ur project but not public to others

any how I learned from $EE here to use it to JUMP OVER  VB Limitations of UDT and string arrays passing from a form to module ...

Hope this helps
0
 
LVL 2

Author Comment

by:jbunting
ID: 6915829
If I move the type to the class, then I'm forced to make it private, correct?

I want the main module to be able to use the type as well because I'm returning it from a class property.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Author Comment

by:jbunting
ID: 6916243
Changing it to Friend does work, but I don't quite understand why.  Since GetPhoneType is a property of the PhoneClass, it seems to me that it should be accessible to any module that has an instance of PhoneClass created in it.

snippets from the msdn library:
Public procedures in a class can be called from anywhere, even by controllers of instances of the class.

Friend makes the procedure visible throughout the project, but not to a controller of an instance of the object.

I thought since I created an instance of PhoneClass in the main module that it would be the "controller of an instance of the object".  At any rate, I thought a public property was always accessible.


0
 
LVL 15

Accepted Solution

by:
ameba earned 100 total points
ID: 6916306
>Changing it to Friend does work, but I don't quite understand why.

Because Friend procedures are not implemented as COM procedures - they do not have to respect all complicated COM rules.

If you want to use PUBLIC property, it is possible to pass UDTs - but UDT must be defined in "Public class module"

>If I move the type to the class, then I'm forced to make it private, correct?

If your project type is Standard EXE, you won't be able to change instancing property of the class - it will be Private.

To have "Public class module", your project type must be ActiveX EXE or DLL.
0
 
LVL 2

Author Comment

by:jbunting
ID: 6916418
I don't have any need to use a Public Property if Friend will work, I'm just trying to understand why it is necessary.

What I don't follow is this though:

The class module is using the UDT definition that is declared in the main (non-class) module.  By virtue of being defined in the main module as public, isn't the UDT therefore a public type? The class module is using the UDT type internally, so it can see it.
0
 
LVL 2

Author Comment

by:jbunting
ID: 6916443
I don't have any need to use a Public Property if Friend will work, I'm just trying to understand why it is necessary.

What I don't follow is this though:

The class module is using the UDT definition that is declared in the main (non-class) module.  By virtue of being defined in the main module as public, isn't the UDT therefore a public type? The class module is using the UDT type internally, so it can see it.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6916455
>being defined in the main module as public, isn't the UDT therefore a public type

Not to the outside world (COM).  If you add reference to EXE containing UDT definitions, you'll see in Object Browser only those UDTs defined in public class modules, those in bas modules are not visible.


If you use Friend, VB can do the passing easier and faster - calling Friend procedures is many times faster, see:
http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=158
0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6916795
Sorry I was out

Hope U satisfied by ameba explanation

Viva EE
:-)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6917069
hearing...
0
 
LVL 2

Author Comment

by:jbunting
ID: 6918294
I posted another question for you Bahnass since you did answer my original question first.

(there's still no way to split points is there?  I haven't been on EE very much in awhile)
0
 
LVL 2

Author Comment

by:jbunting
ID: 6918298
and I forgot to say thank you to ameba for clearing things up for me.
0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6918322
'(there's still no way to split points is there?  I haven't been on EE very much in awhile)

Dear U could send Zero point Question to Community support
thus U can Reduce points to the amount U need then send a new Q with the rest of points

Oh I forgot this for next time !!!!
( but now happy to have Ur points :-) )

0
 
LVL 15

Expert Comment

by:ameba
ID: 6918405
Thanks, jbunting
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
String manipulation in Visual Basic 7 66
Add a task in Outlook from access 11 41
Add and format columns in vb6 7 59
TT Auto Dashboard 13 92
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question