Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arraylist to Array

Status
Not open for further replies.

russgreen

Programmer
Dec 7, 2002
86
GB
I am currently working on a small VB.NET utility that accesses the Microstation VBA object model via COM.

I have this code which I am trying to get to work...

The key bits of information are
Code:
Dim oEl As MicroStationDGN.Element
Dim oTagEl() As MicroStationDGN.TagElement
Dim oArrayList As New ArrayList
oEl in this case will be a cell that contains tags (don't worry what they are...) and I'm using
Code:
oArrayList.AddRange(oEl.GetTags)
to create an ArrayList from the tags that are in oEl (the cell). That works great and
Code:
Debug.WriteLine(oItem.TagDefinitionName.ToString & " - " & oItem.Value)
proves it works.

The problem comes when I try to add the contents of my newly created arraylist to the array of tag elements oTagEl() using
Code:
oTagEl = CType(oArrayList.ToArray(GetType(TagElement)), TagElement())
. I get the follwing error:-

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll
Additional information: At least one element in the source array could not be cast down to the destination array type.

Can anyone see what's going wrong?

TIA

Russ

Code:
Imports System
Imports MicroStationDGN

Module modChangeTags
    Dim frm As frmEditValues

    Public Sub TagValueChange(ByRef oTagEl() As TagElement, ByRef count As Integer)

        If oTagEl(count).TagDefinitionName = "PROJ_TITLE" Then
            If bModifyTags = True Then
                oTagEl(count).Value = frm.txtProjTitle.Text
                oTagEl(count).Rewrite()
            Else
                frm.txtProjTitle.Text = CStr(oTagEl(count).Value)
            End If
        End If

    End Sub

    Public Sub ModelScan(ByRef sCellName As String)
        Dim oScanCriteria As New ElementScanCriteria
        Dim oElEnum As ElementEnumerator
        Dim oEl As Element

        bCellFound = False

        ' Scan only for type 2 Cells
        oScanCriteria.ExcludeAllTypes()
        oScanCriteria.IncludeType(MsdElementType.msdElementTypeCellHeader)

        oElEnum = oMSTN.ActiveModelReference.Scan(oScanCriteria)

        While oElEnum.MoveNext = True
            oEl = DirectCast(oElEnum.Current, Element)

            ' Process only the "titleblock" cell
            If oEl.AsCellElement.Name = sCellName Then
                bCellFound = True

                ' Process the tags
                If oEl.HasAnyTags = True Then
                    ' oTagEl = VB6.CopyArray(oEl.GetTags)

                    ' dimension and populate an arraylist
                    Dim oArrayList As New ArrayList
                    oArrayList.AddRange(oEl.GetTags)

                    ' just check the arraylist has something in it
                    Dim oItem As TagElement
                    For Each oItem In oArrayList
                        Debug.WriteLine(oItem.TagDefinitionName.ToString & " - " & oItem.Value
                    Next

                    ' add the arraylist contents to oTagEl()
                    oTagEl = CType(oArrayList.ToArray(GetType(TagElement)), TagElement())

                    ' Locate each tag by it's definition
                    Dim oCount As Integer
                    For oCount = 0 To UBound(oTagEl)
                        TagValueChange(oTagEl, oCount)
                    Next
                End If
            End If
        End While

        If bModifyTags = True Then
            If bCellFound = True Then
                oMSTN.RedrawAllViews()
            End If
        End If

    End Sub

End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top