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
------------------------------------------------
Private Sub Strutting2DTree(Strut2DDict As Dictionary)
Dim Strut2D As New Strut2DClass
Dim CurrentBranch As String
Dim StrutLength As Double

.
.
.
Strut2DDict.Add CurrentBranch, Strut2D
.
.
.
Strut2DDict(CurrentBranch).StrutLength = StrutLength


Struts may be Vertical or Fan struts and the Dictionary key
CurrentBranch contains the series of struts at the current level, eg
"FF", "FVFV". When the last line of code above is executed for
CurrentBranch "FFF", the .StrutLength is changed for both "FFF" *and*
"FFV" (as witnessed in the Watch window). How can this be??

The other point that *may* be relevant (although I don't see how) is
that this procedure Strutting2DTree can recursively call itself in
certain circumstances (hence the references to "tree" and "branch").

I would very much appreciate any suggestions.

Thanks

Wayne Ivory
Senior Analyst Programmer
Electronic Business Development
Wespine Industries Pty Ltd

Re: Possible bug with VB Scripting Dictionary by Robert

Robert
Fri Sep 16 08:59:32 CDT 2005


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

<wivory@gmail.com> schrieb im Newsbeitrag
news:1126669353.331587.241530@z14g2000cwz.googlegroups.com...
> 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
> ------------------------------------------------
> Private Sub Strutting2DTree(Strut2DDict As Dictionary)
> Dim Strut2D As New Strut2DClass
> Dim CurrentBranch As String
> Dim StrutLength As Double
>
> .
> .
> .
> Strut2DDict.Add CurrentBranch, Strut2D
> .
> .
> .
> Strut2DDict(CurrentBranch).StrutLength = StrutLength
>
>
> Struts may be Vertical or Fan struts and the Dictionary key
> CurrentBranch contains the series of struts at the current level, eg
> "FF", "FVFV". When the last line of code above is executed for
> CurrentBranch "FFF", the .StrutLength is changed for both "FFF" *and*
> "FFV" (as witnessed in the Watch window). How can this be??
>
> The other point that *may* be relevant (although I don't see how) is
> that this procedure Strutting2DTree can recursively call itself in
> certain circumstances (hence the references to "tree" and "branch").
>
> I would very much appreciate any suggestions.
>
> Thanks
>
> Wayne Ivory
> Senior Analyst Programmer
> Electronic Business Development
> Wespine Industries Pty Ltd
>



Re: Possible bug with VB Scripting Dictionary by Michael

Michael
Fri Sep 16 18:21:56 CDT 2005

> Public foodict as new Dictionary
>
> Class fooDataClass
> Public counter
> end Class
>
> sub main()
> dim myinst as new fooDataClass

With the related change...

dim myinst as fooDataClass 'note the removal of new on the dim


>
> 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
>
> <wivory@gmail.com> schrieb im Newsbeitrag
> news:1126669353.331587.241530@z14g2000cwz.googlegroups.com...
>> 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
>> ------------------------------------------------
>> Private Sub Strutting2DTree(Strut2DDict As Dictionary)
>> Dim Strut2D As New Strut2DClass
>> Dim CurrentBranch As String
>> Dim StrutLength As Double
>>
>> .
>> .
>> .
>> Strut2DDict.Add CurrentBranch, Strut2D
>> .
>> .
>> .
>> Strut2DDict(CurrentBranch).StrutLength = StrutLength
>>
>>
>> Struts may be Vertical or Fan struts and the Dictionary key
>> CurrentBranch contains the series of struts at the current level, eg
>> "FF", "FVFV". When the last line of code above is executed for
>> CurrentBranch "FFF", the .StrutLength is changed for both "FFF" *and*
>> "FFV" (as witnessed in the Watch window). How can this be??
>>
>> The other point that *may* be relevant (although I don't see how) is
>> that this procedure Strutting2DTree can recursively call itself in
>> certain circumstances (hence the references to "tree" and "branch").
>>
>> I would very much appreciate any suggestions.
>>
>> Thanks
>>
>> Wayne Ivory
>> Senior Analyst Programmer
>> Electronic Business Development
>> Wespine Industries Pty Ltd

--
Michael Harris
Microsoft MVP Scripting