Arraylist to array

R

Russ Green

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

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 oArrayList.AddRange(oEl.GetTags) to create an ArrayList from the tags that are in oEl (the cell). That works great and 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 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

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
 
A

Armin Zingler

Russ Green said:
The problem comes when I try to add the contents of my newly created
arraylist to the array of tag elements oTagEl() using 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.


I dind't have a very close look but I'd do the following:

instead of
oTagEl = CType(oArrayList.ToArray(GetType(TagElement)), TagElement())
try
dim a as array
a = oArrayList.ToArray(GetType(TagElement))

Is the line executed without an error?

If yes: After the line above has been executed, examine 'a' in the
watch/locals window. What is the type of the elements?

If no: Restart and stop at "a =...". Exmamine oArrayList(0) and other items.
Do they have the correct type (TagElement)?
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top