Results 1 to 11 of 11

Thread: [RESOLVED] Transfer UDT to Class [Nested Classes]

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Resolved [RESOLVED] Transfer UDT to Class [Nested Classes]

    Hello,

    I have a pretty huge UDT but I'm now converting it as a Class Structure. However I'm having issues passing from/to the nested class properties.

    Example:

    Code:
    Dim testDB() As Class1_Main
    
    Private Sub Form_Load()
        ReDim testDB(0)
        Set testDB(0) = New Class1_Main
        testDB(0).Item(0).xName = "test"    'TEST isn't being passed on?
    
        MsgBox testDB(0).Item(0).xName      'EMPTY<<<<<<<<<<<<<
    
        End
    End Sub
    I've attached the Simple Project.
    Any help would be appreciated.
    Attached Files Attached Files
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,052

    Re: Transfer UDT to Class [Nested Classes]

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:21 AM.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Transfer UDT to Class [Nested Classes]

    .Item(n) returns a clone, non-permanent class instance.

    So, testDB(0).Item(0).xName = "test" is setting xName on the clone

    MsgBox testDB(0).Item(0).xName is creating another, different, clone

    Neither clone is persisted. I didn't run your test, just reviewed the class files

    If this:
    Code:
    Public Property Get Item(ByVal iIndex As Byte) As Class2_Items
       Set Item = xItem(iIndex).Clone
    End Property
    changed to this, then you should get your value
    Code:
    Public Property Get Item(ByVal iIndex As Byte) As Class2_Items
       Set Item = xItem(iIndex)
    End Property
    Edited: too slow, dz32 beat me to it
    Last edited by LaVolpe; May 14th, 2018 at 07:09 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Transfer UDT to Class [Nested Classes]

    Have you tried setting break points in the class and stepping through the code to see what gets executed?

  5. #5

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Re: Transfer UDT to Class [Nested Classes]

    Ohh i see.. So no need for the Clone Function.
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: [RESOLVED] Transfer UDT to Class [Nested Classes]

    Yeah, I just thought your code through too, and LaVolpe is right on. The Clone function down in Class2_Items is very confusing.

    The following line in your Form1 ...

    Code:
    testDB(0).Item(0).xName = "test"    'TEST isn't being passed on?
    ... the highlighted part is calling the Clone function. Remember that only the last piece of a statement is the Let/Set piece (the .xName piece). All other pieces will be Get pieces. Therefore, Item(0) will be a Get. And then, in your Clone procedure, you've got ...

    Code:
    Set Clone = New Class2_Items
    ... within that procedure. Therefore, this local Clone variable/object is uninstantiated once you're finished using it. Therefore, you've set the .xName property in a temporary (further nested, somewhat recursive) version of Class2_Items. Once the testDB(0).Item(0).xName = "test" statement completes, that Clone is destroyed, along with its .xName property.

    If you start your program with F8 and just step through it, you'll see all of this.

    I'm not clear on why you just didn't directly return the items in Class2 rather than trying to clone them. Are you trying to make a read-only version of them? That would be accomplished a bit differently.

    The easiest way I could see to do this would be to just return the entire Class2 as a property of Class1. Something like the following ...

    Code:
    Friend Property Get Item(ByVal iIndex As Byte) As Class2_Items
        ' The item with iIndex must have been added before fetching with iIndex.
        Set Item = xItem(iIndex)
    End Property
    
    Friend Property Set Item(ByVal iIndex As Byte, newItem As Class2_Items)
        ' The item with iIndex must have been added before being set with newItem.
        Set xItem(iIndex) = newItem
    End Property
    ... (aircode, I didn't test) that should just directly return the item with no cloning.

    Good Luck,
    Elroy

    EDIT1: Ahhh, sorry, I took a bit to compose my post. Yeah, no need for your clone procedure.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,052

    Re: [RESOLVED] Transfer UDT to Class [Nested Classes]

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:21 AM.

  8. #8

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Re: [RESOLVED] Transfer UDT to Class [Nested Classes]

    Although this issue has been resolved, I cam across another interesting thing.

    I have another property which is a ByteArray [xData]

    But I can't seem to access it directly without making a temporary variable to dump the data.
    Is that normal?

    i.e
    Code:
    Private Sub Form_Load()
        ReDim testDB(0)
        Set testDB(0) = New Class1_Main
        testDB(0).Item(0).xData = StrConv("test", vbFromUnicode)
        
        Debug.Print testDB(0).Item(0).xData(2)    '//Error here <<<<
    
        End
    End Sub
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Transfer UDT to Class [Nested Classes]

    To reference it directly... notice the closed parentheses
    Code:
    Debug.Print testDB(0).Item(0).xData()(2)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Re: [RESOLVED] Transfer UDT to Class [Nested Classes]

    Quote Originally Posted by LaVolpe View Post
    To reference it directly... notice the closed parentheses
    Code:
    Debug.Print testDB(0).Item(0).xData()(2)
    hmm... never seen a reference like this .. good to know

    Although there is somewhat a speed impact....
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  11. #11
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,052

    Re: [RESOLVED] Transfer UDT to Class [Nested Classes]

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:21 AM.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width