473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Arraylist to array

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.msdElemen tTypeCellHeader)

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
Nov 20 '05 #1
2 6838
"Russ Green" <rg*******@SPAM-stubbsrich.com> schrieb
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)?
--
Armin
Nov 20 '05 #2
Thanks.

I'll try this and maybe get back to you.

Russ
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Russ Green" <rg*******@SPAM-stubbsrich.com> schrieb
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)?
--
Armin

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
7
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
6
by: rdi | last post by:
I have 3 classes: mailBox, mailFilter & mailFilterSet I then have an arraylist. Each element in the array is of type mailBox (variable name is mbox). One item of the mailbox class is an...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
5
by: Paul | last post by:
Off the cuff, does anyone know if arraylist is more efficeint at adding items to an array than redim preserve? Paul <begin loop> Dim c As Integer = SomeArray.GetUpperBound(0) + 1 ReDim...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.