Author Topic: What is wrong with this class???  (Read 2331 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
What is wrong with this class???
« on: May 24, 2007, 10:35:55 AM »
I am writting an app to collect attributes from a block.  I have a simple class named AttInf (see below).  In a form module I have a procedure that finds the insertion, and adds each attribute to another class instance that is not much more than a collection of AttInf instances (see class Attributes, below).

The issue I'm having is with the Add method in the Attributes class.  The routine that cycles through the attributes works fine, calling the Add method of the Attributes class with the correct argument each time.  However, when I step into the Add method, each attinf in the atts collection takes on the properties of the attinf sent as the argument to the add method.  I don't understand how this can happen, nor what to do to solve the problem.

Suggestions are much appreciated.

Mike Weaver


Class AttInf
Code: [Select]
Option Explicit

Public TagString As String
Public Value As String
Public Handle As String
'Simple enough?

Calling Procedure
Code: [Select]
  For Each objTarget In objPSpace
    If objTarget.ObjectName = "AcDbBlockReference" Then
    If (objTarget.Name = InsertName) And (objTarget.HasAttributes = True) Then
       
        Dim Atts As Variant
        Atts = objTarget.GetAttributes
        Dim i As Integer
        For i = LBound(Atts) To UBound(Atts)
          Set ThisAtt = Atts(i)
          attThis.TagString = ThisAtt.TagString
          attThis.Value = ThisAtt.TextString
          attThis.Handle = ThisAtt.Handle
          Attribs.Add attThis '<<<<<<<<<<<<<<<---------------------<<<  This is where I'm having the problem
        Next i
        GetAttributeInfo.PathName = PathName
        GetAttributeInfo.Attribs = Attribs
        Exit Function
        End If
    End If
  Next objTarget

Class Attributes
Code: [Select]
Option Explicit

Private Atts As collection

Private Sub Class_Initialize()
  Set Atts = New collection
End Sub
Private Sub Class_Terminate()
  Set Atts = Nothing
End Sub

...

Public Sub Add(AttNew As AttInf)
  Dim i As Integer
  For i = 1 To Atts.Count
    If Atts.Item(i).TagString = AttNew.TagString And Atts.Item(i).Handle = AttNew.Handle Then
    Atts.Item(i).Value = AttNew.Value
    Else
    Atts.Add AttNew
    Exit Sub
    End If
  Next i
  Atts.Add AttNew
End Sub


Arizona

  • Guest
Re: What is wrong with this class???
« Reply #1 on: May 24, 2007, 11:25:41 AM »
This might help...

Code: [Select]
    If objOldBlk.HasAttributes Then
              varOldAtt = objOldBlk.GetAttributes     'get old block attributes
            End If
            For intOldCnt = LBound(varOldAtt) To UBound(varOldAtt)
              Set objOldAttRef = varOldAtt(intOldCnt)
            Next intOldCnt

…...................................

   Set objNewBlk = ThisDrawing.ModelSpace.InsertBlock(InsertPt, NewBlkName, xScale, yScale, zScale, Rot) 'insert new tblock
     
   varNewAtt = objNewBlk.GetAttributes 'get attributes

   For intOldCnt = LBound(varOldAtt) To UBound(varOldAtt)
       For intNewCnt = LBound(varNewAtt) To UBound(varNewAtt)
            If varNewAtt(intNewCnt).TagString = varOldAtt(intOldCnt).TagString Then
                varNewAtt(intNewCnt).TextString = varOldAtt(intOldCnt).TextString
            End If
      Next
   Next

This got attribute values from an old titleblock and replaced the block and then replaced the attribute values..

mkweaver

  • Bull Frog
  • Posts: 352
Re: What is wrong with this class???
« Reply #2 on: May 24, 2007, 04:35:56 PM »
with a bit of help from a co-worker I discovered the following.  My routine that calls the add method of my Attributes class is instantiated outside my loop, then modified each time through the loop to give it the values for the current attribute.  My add method is taking this AttInf instance and adding it to my collection.  The problem is that it is adding a reference to the same AttInf each time - which changes each time through the loop.  Thus, my collection is simply a collection of references to the same AttInf.  duh.

I can now solve my problem.

Mike Weaver