Possible bug with VB Scripting Dictionary
CADForums.net Forum Index CADForums.net
Discussion of AutoCAD and other CAD software.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web cadforums.net
Possible bug with VB Scripting Dictionary

 
Post new topic   Reply to topic    CADForums.net Forum Index -> VBA
Author Message
Guest






Posted: Wed Sep 14, 2005 8:10 am    Post subject: Possible bug with VB Scripting Dictionary Reply with quote

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

Back to top
Robert Baur
Guest





Posted: Fri Sep 16, 2005 4:10 pm    Post subject: Re: Possible bug with VB Scripting Dictionary Reply with quote

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
Back to top
Michael Harris (MVP)
Guest





Posted: Sat Sep 17, 2005 12:10 am    Post subject: Re: Possible bug with VB Scripting Dictionary Reply with quote

Quote:
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

--
Michael Harris
Microsoft MVP Scripting

Back to top
 
Post new topic   Reply to topic    CADForums.net Forum Index -> VBA All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Windows Server DSP VoIP Electronics New Topics
Powered by phpBB