+ Reply to Thread
Results 1 to 9 of 9

How do I reference classes that have a relationship with each other

  1. #1
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Question How do I reference classes that have a relationship with each other

    Hi all, been a wee while.

    I'm trying to figure out how to reference one class within another that has a reference back to the first class again. For example, ActiveSheet has a Parent property, which has an ActiveSheet property, which has a Parent property, and so on. I want to do something similar.

    I want to create two classes, one that stores details of an Organisation and one that stores details of a Department. One of the properties of an Org should return the Dept class that the Org belongs to (one Org has one Dept) and one of the properties of a Dept should be an array (or collection?) of Org classes that belong to it (one Dept has many Orgs). But I can't figure out the logic without causing a stack error.

    Anybody any experience they can lend me here?

    Thanks all.
    Please click *Add Reputation if I've helped

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: How do I reference classes that have a relationship with each other

    Hi there,

    It's very difficult to be precise about this without having a lot more information, but what goes through my head is that the "Organisation" class should have a Let property which allows an instance of the "Department" class to be passed to it.

    Similarly, the "Department" class should have a Let property which allows an instance of the "Organisation" class to be passed to it.

    Hope this helps,

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: How do I reference classes that have a relationship with each other

    If you're happy taking me through it Greg, I can give you whatever info you need. Also, if you realise I'm going about this completely the wrong way let me know.

    I use my PERSONAL.XLSB extensively, the procedures within often repeating each other in parts. I'm trying to overhaul all this, using objects effectively. Most of the procedures need to be able to determine details about the organisations we have on the books so I'm starting there. The classes I want to create are effectively going to be used to store constants, in that the data within them won't change.

    So, the way I was thinking was to have classes to store individual orgs and a further class that acts as a collection of the orgs. It will be this collection class that I then reference in my code using something like

    Please Login or Register  to view this content.
    To build the collection, the Class_Initialize for clsOrgs calls an Add procedure for each org that we have on the books, with the data for each org as the arguments. It was when I was repeating the dept info for several orgs that I realised that the dept should probably be an object in it's own right. And since there are several departments, they should also have a collection class. That was my thinking in terms of the concept anyway, and attached is where I'm up to in terms of the coding for it. I've sanitised the data in the Class_Initialize, but what is there is representative of how the orgs and depts relate to each other.
    Attached Files Attached Files
    Last edited by kadeo; 10-17-2019 at 05:48 AM.

  4. #4
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: How do I reference classes that have a relationship with each other

    Here's what I tried. It might get you started. Code for clsOrg:

    Please Login or Register  to view this content.
    Code for clsDept:

    Please Login or Register  to view this content.
    Then a quick test macro:

    Please Login or Register  to view this content.
    WBD
    Office 365 on Windows 11, looking for rep!

  5. #5
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: How do I reference classes that have a relationship with each other

    Dix, could you take a look at the attachment. I think I was editing that post to include the attcahment when you posted yours. I'd very much appreciate your guidance too.

  6. #6
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: How do I reference classes that have a relationship with each other

    Hmmm. You're capturing the dept/org relationship twice which is confusing. Let me think about this.

    WBD

  7. #7
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: How do I reference classes that have a relationship with each other

    OK. I think you had too many objects kicking around. I've tried to simplify this a bit. See attached which is based on my previous suggestion but has kept most of your code. Look in the modTest module.

    WBD
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: How do I reference classes that have a relationship with each other

    Dix, thanks so much for spending some time on this. I just realised you'd replied. I'm off work atm so I'll digest it properly when I get back.

    Initially though I can see that the clsOrgs data is loaded into the class from outside the class module. Is there no way to do that from within the class? I'd like to make these class modules easy to supplant into other projects without having to dig out where the data is loaded from too. In a perfect world, I'd just like to use the New keyword to create the object, and all the data would be there.

    Thanks again man.

  9. #9
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: How do I reference classes that have a relationship with each other

    I think your main problem was that you were trying to create the relationships entirely in the class constructors but that didn't then correctly encapsulate the relationship between the classes.

    WBD

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA - Help with classes -
    By ironfelix717 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-10-2018, 03:59 AM
  2. [SOLVED] VBA Classes For ADO
    By NeedForExcel in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 08-14-2015, 11:07 AM
  3. [SOLVED] Get how many classes I've taken so far.
    By vbmark in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-20-2014, 11:15 AM
  4. T-value for different classes
    By dr.cuco in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-14-2013, 07:16 PM
  5. Replies: 5
    Last Post: 11-04-2013, 11:28 AM
  6. Reference : Lotus Notes Automation Classes
    By Patrick in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-01-2005, 10:05 AM
  7. Using Classes in .xla
    By oli in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-03-2005, 06:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1