Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Objects as Properties in VBA - possible?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chadwick - 29 Feb 2008 21:05 GMT
Hello all,
  Is it possible for a property of an object in VBA to itself be an
object?  I'm working on a program that involves chemical equations,
and consequently I have defined a couple of useful classes:
"ChemicalCompound," and "ChemicalEquation."  The ChemicalEquation
class has as one of its members an array of ChemicalCompound objects,
which represent the reactant compounds in the equation.  I would like
to keep the actual array private (i.e. access restricted to the class)
and use a Property Get to access the ChemicalCompound objects in the
array.  Is possible for the return value of a Property Get to be an
object of a user-defined class?

Here's some rough code that might give you a sense of what I'm trying
to do:

In Class Module ChemicalEquation:

Private Type ChemicalCompoundsList
   'Note that the nth coefficient goes with the nth compound
   Compounds() As ChemicalCompound
   Coefficients() As Integer
End Type

Private pReactants As ChemicalCompoundsList
Private pProducts As ChemicalCompoundsList

'Can I do this?:
Property Get ReactantCompound(Index As Integer)
   ReactantCompound = pReactants.Compounds(Index - 1)
End Property

In a Standard Module:
Sub TestChemicalEquationClass()
   'Create a ChemicalEquation Object
   Dim MyEquation As ChemicalEquation
   Set MyEquation = New ChemicalEquation

   'Retrieve the 1st ChemicalCompound object in the pReactants array,
   'via Property Get ReactantCompound
   Dim MyCompound As ChemicalCompound
   MyCompound = MyEquation.ReactantCompound(1)
End Sub

Any takers?  Help is always greatly appreciated.
Jonathan West - 29 Feb 2008 21:41 GMT
> Hello all,
>   Is it possible for a property of an object in VBA to itself be an
[quoted text clipped - 7 lines]
> array.  Is possible for the return value of a Property Get to be an
> object of a user-defined class?

Yes. The syntax you use is like this

Property Get ReactantCompound(Index As Integer) As ChemicalCompound

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

chadwick - 01 Mar 2008 07:17 GMT
> > Hello all,
> >   Is it possible for a property of an object in VBA to itself be an
[quoted text clipped - 16 lines]
> Jonathan West - Word MVPwww.intelligentdocuments.co.uk
> Please reply to the newsgroup

Thanks so much for the help!  Sometimes it's the simplest things...

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.