+ Reply to Thread
Results 1 to 6 of 6

Converting nested UDTs to Classes

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Converting nested UDTs to Classes

    Intro/Background (Feel free to skip if makes it harder to follow)

    I have this hobby project which, due to complexity & number of subprocedures, I found I was adding a lot of global variables (to avoid repeatedly passing these as arguments down the subprocedure tree).

    As the number of global variables grew, they became difficult to manage so I converted them into user-defined types. This worked great for keeping related variables together and intellisense.

    Then I found that some were repeatedly using the same base functions so I thought I should look at converting these UDTs & functions into a class. (I am new to classes & OOP. On the other hand I have been creating complex user forms for years so I do have some understanding of encapsulation & get/set/let properties etc.)



    The Problem

    I converted my primary UDT into a class. Only to discover that doing this broke all the UDTs that were nested in this UDT. Online searches taught me that I can't have a public UDT inside a class.

    What I think I need to do is to convert these "sub" UDTs into "sub-classes" of the new class - if such a thing is possible in VBA? I have come across the word "Implements" in VBA & Classes which sounds promising but I don't know if this is what I need?


    If it helps, I have included an extract below of my 'before' code. When I convert THISMACRO to a class, it breaks the dependent/nested UDTs THISMACROSETUP and THISMACRORESULTS.

    Please Login or Register  to view this content.
    Last edited by mc84excel; 06-16-2019 at 07:16 PM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Converting nested UDTs to Classes

    The simplest way is by defining public variables name of the classes, same as the properties of the UDTs

    Here are the steps :
    - Add 3 ClassModules (Menu "Insert" --> "ClassModules")

    - Rename each ClassModule to "THISMACRO", "THISMACROSETUP", "THISMACRORESULTS" (same as the name of the UDTs)
    To rename the ClassModule, click the ClassModule on the left pane, then press F4 on keyboard, and change the "(name)" property

    - Put this code on ClassModule "THISMACRO" :
    Please Login or Register  to view this content.
    - Put this code on ClassModule "THISMACRO" :
    Please Login or Register  to view this content.
    - Put this code on ClassModule "THISMACRO" :
    Please Login or Register  to view this content.
    - To test the classes, on a standard module, put this sub :
    Please Login or Register  to view this content.
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Converting nested UDTs to Classes

    Thanks karedog (reps)

    I've since run into two more problems:
    1. I cant use a class variable as the counter of a loop
    2. My class global array gives a compile error on the Redim Preserve. "Method or data member not found"

    I've solved the first issue but I'm stumped on the 2nd.

    Anybody able to assist with the array compile issue?

    Below is the relevant code in my main class module:
    Please Login or Register  to view this content.
    And here is the extract from one of my standard modules that gives the compile error. It worked fine before I began converting my nested UDTs to classes.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Converting nested UDTs to Classes

    maybe I'm missing something... but....

    never mind I did miss something
    Last edited by scottiex; 06-18-2019 at 12:56 AM.
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Converting nested UDTs to Classes

    You are welcome, thanks for reps.

    Basically, a property (gavarSource in this case), is just a "function" that acts as the interface from outside world (outside the class) with inside world (inside the class, for example the private variables of this class), using Let/Set/Get statement.

    Since it is just a "function", you cannot doing something like Redim, etc to a function (you can do this to a variable, not a function).

    If you choose using a 'simple assigning of Class Property' way, and you are working with a variant variable that contain array(s), the easiest way is using a temporary variable to get/let from/to the property.
    Please Login or Register  to view this content.
    If from the beginning you plan that the gavarSource property must be an array, we can avoid using temporary variable, but we need to add some additional parameters to property Let and property Get, and also add public procedures to init and redim the mavarSource variable.

    But in this case I think you plan to use the gavarSource/mavarSource as global variable (could be contained anything, from simple variables like number,string,date,etc) as well as array, so it's better to use temporary variable approach.

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Talking Re: Converting nested UDTs to Classes

    Quote Originally Posted by karedog View Post
    You are welcome, thanks for reps.
    Glad to do so. Your answer really helped me out.


    Quote Originally Posted by karedog View Post
    Basically, a property (gavarSource in this case), is just a "function" that acts as the interface from outside world (outside the class) with inside world (inside the class, for example the private variables of this class), using Let/Set/Get statement.

    Since it is just a "function", you cannot doing something like Redim, etc to a function (you can do this to a variable, not a function).
    Yes, I know what a property is. I had never tried to reference an array from outside its class before so I was temporarily confused. You are right of course - you cant redim a function or property.

    I put the Redim Preserve into a public function of the class and then I call this function wherever the standard modules want to redim preserve the global array. Fixed.


    Quote Originally Posted by karedog View Post
    But in this case I think you plan to use the gavarSource/mavarSource as global variable (could be contained anything, from simple variables like number,string,date,etc) as well as array, so it's better to use temporary variable approach.
    Ha ha, no. I do have some bad coding habits but I don't like to switch around the usage of a variable like that!

    I'm afraid I have this bad habit of declaring my array variables as a variant. This gives me flexibility by not restricting the array to one data type (e.g. 2d array and each column may contain a different data type)
    Also - By declaring this variable as a variant instead of variant(), I can very quickly test if the function had set the array by using IsArray on the variable. That wouldn't be possible if I had used variant() as it would always return True. (Instead I would need to use a custom function for checking if the array was empty. Doing that is not as fast as testing IsArray on a variable.)

+ 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. [SOLVED] What's wrong with my nested ifs? Converting numbers to time
    By kyles123 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-28-2015, 11:56 AM
  2. Problem converting a very simple nested IF function to a UDF
    By GabrielG in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-23-2015, 09:53 AM
  3. [SOLVED] Converting nested IFs into VBA function
    By dtipitino in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-21-2014, 10:19 AM
  4. Beginner to User Defined Data Types (Custom Type, UDTs)
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 11-24-2013, 06:21 PM
  5. Replies: 5
    Last Post: 11-04-2013, 11:28 AM
  6. Converting nested if to Macro
    By Cooljazz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-21-2011, 01:37 AM
  7. Passing UDTs to subs
    By PeterWilliams in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-27-2009, 04:45 PM

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