Possible bug with VB Scripting Dictionary

Discussion in 'AutoCAD' started by wivory, Sep 14, 2005.

  1. wivory

    wivory Guest

    Hard to know how to classify this question so apologies for the
    cross-post.

    I am writing an application using VBA Retail 6.4.9972 over VB 6.3
    inside AutoCAD 2002 (Wait! Don't abandon this question VB programmers
    - you really don't need to know anything about AutoCAD in order to
    help). It's a fairly sizeable application (10,000+ lines) and is
    working quite well but this thing has had me pulling my hair out since
    yesterday.

    I am using a Dictionary to store information about roof struts but I've
    found that modifying the Item for a particular key modifies not only
    the intended item, but also another (seemingly unrelated) item!! It's
    true! I've stepped through line by line and can see in the Watch
    window that they *both* change when the one assignment line is
    executed. Now the data structure is not straightforward and it may be
    related to that but I've tried various modifications on the theme and
    the same problem keeps happening.

    Here's the relevant code:

    In a class module named Strut2DClass
    ------------------------------------
    Public MaxPosition
    Public MinPosition
    Public MinHalfSpread As Double
    Public ActualPosition
    Public ActualHalfSpread As Double
    Public LineHandle As String
    Public StrutLength As Long
    Public ParentDetails As New Dictionary

    In the standard AutoCAD module named ThisDrawing
     
    wivory, Sep 14, 2005
    #1
  2. wivory

    Robert Baur Guest

    Hi,

    so only a thought

    your item in the Dictionary the referenc to class Strut2DClass that you
    construct on the local heap of the function once!
    Then you add this REFERENC as item for the key "FF" so if you add an
    additional key and use your class instance Strut2D also for the second
    insertion, changing the properties of the item class will affect the class
    instance therefor this will look like both are change but there is only one
    instance that is changed!
    Here a example which shows as code what i intended to deliver:

    Public foodict as new Dictionary

    Class fooDataClass
    Public counter
    end Class

    sub main()
    dim myinst as new fooDataClass

    myinst.counter = 1
    foodict.Add "1", myinst

    ' for resolving this problem a new instance must be generated by using
    following line
    ' Set mylist = new fooDataClass
    myinst.counter = 2 'this will also change the value of the item of
    key "1"
    foodict.Add "2", myinst

    myinst("1").counter = 1 'this will also chnage the value of the item
    from key "2"

    end sub

    Regards,

    Rob
     
    Robert Baur, Sep 16, 2005
    #2
  3. Public foodict as new Dictionary
    With the related change...

    dim myinst as fooDataClass 'note the removal of new on the dim
     
    Michael Harris \(MVP\), Sep 17, 2005
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.