Results 1 to 14 of 14

Thread: collection of classes with collection of classes

  1. #1

    Thread Starter
    Lively Member Kersey's Avatar
    Join Date
    Jun 1999
    Location
    The Netherlands
    Posts
    101
    Hello World,

    Once again I'm in need for your help....

    I've built a collection of my own class which also holds a collection with onather class.
    so i can do something like this :
    colalfa(2).colbeta(3).text = "test"

    from a db I read a lot of records with data like this:
    alfanr betanr strtext
    1 4 aaa
    56 78 bbb
    46 2 ccc

    I want to add this data to my collection.

    something like:
    colalfa(alfanr).colbeta(betanr).text=strtext

    how can i check if the colalfa(alfanr).colbeta(betanr) is already created ???
    because if it does not exist yet i have to add it to my collection.


    Greetinx,

    Don

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can simply try to access it. If it doesn't exist you will get a trappable error.
    Code:
    'this code assumes you've read the db
    'and that the result is in a RecordSet object
    'called rs
    On Error Resume Next
    colalfa(rs!alfanr).colbeta(rs!betanr).Text = rs!strtext
    If Err Then
        'the item doesn't exist so create it here
    End If
    Good luck!

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Without errorhandling, you can try to compare with IS
    Code:
    IF colalfa(rs!alfanr).colbeta(rs!betanr) Is nothing then
    'does not exist
    end if
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Lively Member Kersey's Avatar
    Join Date
    Jun 1999
    Location
    The Netherlands
    Posts
    101
    kedaman's solution:

    when trying this error occurs in the following code:

    Code:
    Public Property Get Item(vntIndexKey As Variant) As colbeta
        'used when referencing an element in the collection
        'vntIndexKey contains either the Index or Key to the collection,
        'this is why it is declared as a Variant
        'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
      Set Item = mCol(vntIndexKey)
    End Property
    how should i handle this ???


    [Edited by Kersey on 09-07-2000 at 05:25 AM]

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    No! You don't have to catch the error within the class.
    VB will raise an error when you're trying to access an item that doesn't exist.

  6. #6

    Thread Starter
    Lively Member Kersey's Avatar
    Join Date
    Jun 1999
    Location
    The Netherlands
    Posts
    101

    to Joachim

    thanx
    think YOUR solution will work just fine...

    actually i was trying Kedaman's solution alredy but couldn't fix this error so if anyone would still reply ????....

    Don

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: to Joachim

    Originally posted by Kersey
    thanx
    think YOUR solution will work just fine...

    actually i was trying Kedaman's solution alredy but couldn't fix this error so if anyone would still reply ????....

    Don
    why wouldn't you be going for guru?

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    The problem is that Joacims solution resumes everytime an error occur, which is probably why the error didn't appear at all.

    Now in the property get statement, put the set statement in the else part of a if statement comparing the reference with nothing again.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9

    Thread Starter
    Lively Member Kersey's Avatar
    Join Date
    Jun 1999
    Location
    The Netherlands
    Posts
    101
    so I'll check for an error in
    Code:
    Public Property Get Item(vntIndexKey As Variant) As colbeta
        'used when referencing an element in the collection
        'vntIndexKey contains either the Index or Key to the collection,
        'this is why it is declared as a Variant
        'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
      Set Item = mCol(vntIndexKey)
    End Property
    and set it to nothing if the err occurs.

    so the object = nothing

    to a_silvy :

    I'm already working as a professional VB-developper, all that counts on your CV (at the end of the month this equals some amount of money in your wallet) is your MSDN & other diploma's , project experience ( finish them WELL & in time).
    Who on earth cares if you're a vb-world guru (with respect to them who have the time skill and knowledge to reach this)??? ( I don't have the time !!!!)
    When your good you will have to prove it in real life,
    where other skills or even more important than programming Vb.
    This does not mean I'm only here to ask question get the answer a go my own way ( Cashing in others peoples solutions).
    Instead for every question I ask i try to answer at least one person as good as possible.
    Because Togheter we can make better app's than each of us alone !

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Again: No error checkin needed

    You just use the same technique again:
    Code:
    if not mcol(vntindexkey) is nothing then
     set item=mcol(vntindexkey)
    end if
    the item will remain nothing if theres no reference
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11

    Thread Starter
    Lively Member Kersey's Avatar
    Join Date
    Jun 1999
    Location
    The Netherlands
    Posts
    101

    Error when not error checking

    Dear Kedaman,

    using following code (as suggested):

    Code:
    Public Property Get Item(vntIndexKey As Variant) As Gas
        'used when referencing an element in the collection
        'vntIndexKey contains either the Index or Key to the collection,
        'this is why it is declared as a Variant
        'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
        If Not mCol(vntIndexKey) Is Nothing Then
            Set Item = mCol(vntIndexKey)
        End If
          
    End Property
    does generate following error

    run-time error 5 : invalid procedure call or argument

    at the line
    Code:
    If Not mCol(vntIndexKey) Is Nothing Then
    because You cant use mcol(vntIndexKey) when vntIndexKey does not exist in the collection !!!!

    So I think, I have to check for an error right here....

    Unless someone else disagrees with me and supplies a solution for this

    IT MIGHT BE YOU.....POST IT (please ?)


    greetinx,


    Don

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You mean that you pass a key, instead of a index? eh, the error should be with block or variable not set, but i'm not sure, so anyway as it didn't work, you could use error trapping instead, to be sure use:
    on local error resume next

    shouldn't affect any error handling outside the class.
    The property will still return an empty reference so you should use the is comparation method out there.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13

    Thread Starter
    Lively Member Kersey's Avatar
    Join Date
    Jun 1999
    Location
    The Netherlands
    Posts
    101
    when passing a not existing index I get a subscript out of range !!!!

    still testing da lot
    so far this works fine:
    Code:
    Public Property Get Item(vntIndexKey As Variant) As Gas
        'used when referencing an element in the collection
        'vntIndexKey contains either the Index or Key to the collection,
        'this is why it is declared as a Variant
        'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
        On Local Error Resume Next
            Set Item = mCol(vntIndexKey)
    End Property
    Thanks for your Contribution !!!
    (especially : KeDaMaN & JoAcHiM A.)


    Don.

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That should avoid everything, you could use ubound, lbound, abs and int to avoid the SOOR error, but this time errorhandling wins the game
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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