+ Reply to Thread
Results 1 to 8 of 8

Set obj = New TypeOf(obj)

  1. #1
    Registered User
    Join Date
    03-23-2015
    Location
    UK
    MS-Off Ver
    2016
    Posts
    27

    Set obj = New TypeOf(obj)

    I've setup a couple of table classes which have references to the Worksheet and the Table they belong to.


    Table Base Class:
    Please Login or Register  to view this content.
    Table(s) Class:
    Please Login or Register  to view this content.

    All of the table classes have this similar structure, so I'd like to setup a SubRoutine which Initializes these classes.


    I already have the classes strongly declared:
    Please Login or Register  to view this content.
    Heres what I'm currently doing (it works!):
    Please Login or Register  to view this content.

    And heres what I'm trying to do:
    Please Login or Register  to view this content.

    VBA does not understand the TypeOf Identifier, anyone help to correct or point me in the right direction with this?


    Any help would be greatly appreciated.

    Thanks.
    Last edited by pizzaboyuk; 07-14-2018 at 01:32 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-13-2008
    Location
    Καθαρτήριο
    MS-Off Ver
    2010 primarily
    Posts
    557

    Re: Set obj = New TypeOf(obj)

    You can't do that and I don't really follow from what you've posted why you would need to - if you're passing a variable of the relevant type, why can't you just use it? The closest thing I think to what you're doing would be an Interface class - if you have multiple classes with similar characteristics, you should certainly look into it.

  3. #3
    Registered User
    Join Date
    03-23-2015
    Location
    UK
    MS-Off Ver
    2016
    Posts
    27

    Re: Set obj = New TypeOf(obj)

    Does Excel VBA support Interfaces? VBA doesn't support Inheritance natively right?

    I'm just trying to simplify the readability of my code. As all the "Inherited" classes MUST call the Initialize() subroutine to setup the Table and Worksheet references, I want to combine the instantiation code with the Initialize() call.

    Instead of having a huge SELECT statement in the SetReferences() routine against TypeOf(obj), I just wondered whether this would be possible...
    Last edited by pizzaboyuk; 07-14-2018 at 05:08 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    08-13-2008
    Location
    Καθαρτήριο
    MS-Off Ver
    2010 primarily
    Posts
    557

    Re: Set obj = New TypeOf(obj)

    Yes, it supports interfaces, but not inheritance. Each class has to explicitly implement the code for the interface methods/properties. Although from what you've posted, I don't really see why you can't just use something simple like:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-23-2015
    Location
    UK
    MS-Off Ver
    2016
    Posts
    27

    Re: Set obj = New TypeOf(obj)

    Thanks Rorya.

    I am already doing something as you mentioned:
    Please Login or Register  to view this content.
    I just wanted a quicker way to see just the Table and Worksheet references which are getting passed to the class without the Set statement for easy verification if I had to move the Excel Table to another worksheet.

    I just presumed there was a way to resolve the
    Please Login or Register  to view this content.
    with GetType, TypeOf or TypeName...

    Guess my workaround of having the declaration statement include the SET Instantiation for the Table class and then having my Initialize() routine called somewhere else will have to do...

    Cheers

  6. #6
    Valued Forum Contributor
    Join Date
    08-13-2008
    Location
    Καθαρτήριο
    MS-Off Ver
    2010 primarily
    Posts
    557

    Re: Set obj = New TypeOf(obj)

    That still doesn't make any sense to me. How could TypeOf obj work, unless you had already set obj to the relevant type?

  7. #7
    Registered User
    Join Date
    03-23-2015
    Location
    UK
    MS-Off Ver
    2016
    Posts
    27

    Re: Set obj = New TypeOf(obj)

    Quote Originally Posted by rorya View Post
    How could TypeOf obj work, unless you had already set obj to the relevant type?
    That was what I was asking help on... to correct the syntax. I just used TypeOf as an example presuming a more experienced user would be able to assume what I was trying to do and be able to give me the correct way to code it.

    Quote Originally Posted by pizzaboyuk View Post
    VBA does not understand the TypeOf Identifier, anyone help to correct or point me in the right direction with this?

    What did you think I was asking help on?

    I gave an example what I was ALREADY doing. I gave an example of WHAT I wanted to do?
    Last edited by pizzaboyuk; 07-14-2018 at 12:23 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    08-13-2008
    Location
    Καθαρτήριο
    MS-Off Ver
    2010 primarily
    Posts
    557

    Re: Set obj = New TypeOf(obj)

    Honestly I don’t know what you’re asking. How should the code know what type you want?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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