473,569 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.AddR ange(oEl.GetTag s) to create an ArrayList from the tags that are in oEl (the cell). That works great and Debug.WriteLine (oItem.TagDefin itionName.ToStr ing & " - " & 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(oArrayLis t.ToArray(GetTy pe(TagElement)) , TagElement()). I get the follwing error:-

An unhandled exception of type 'System.Invalid CastException' 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).T agDefinitionNam e = "PROJ_TITLE " Then
If bModifyTags = True Then
oTagEl(count).V alue = frm.txtProjTitl e.Text
oTagEl(count).R ewrite()
Else
frm.txtProjTitl e.Text = CStr(oTagEl(cou nt).Value)
End If
End If

End Sub

Public Sub ModelScan(ByRef sCellName As String)
Dim oScanCriteria As New ElementScanCrit eria
Dim oElEnum As ElementEnumerat or
Dim oEl As Element

bCellFound = False

' Scan only for type 2 Cells
oScanCriteria.E xcludeAllTypes( )
oScanCriteria.I ncludeType(MsdE lementType.msdE lementTypeCellH eader)

oElEnum = oMSTN.ActiveMod elReference.Sca n(oScanCriteria )

While oElEnum.MoveNex t = True
oEl = DirectCast(oElE num.Current, Element)

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

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

' dimension and populate an arraylist
Dim oArrayList As New ArrayList
oArrayList.AddR ange(oEl.GetTag s)

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

' add the arraylist contents to oTagEl()
oTagEl = CType(oArrayLis t.ToArray(GetTy pe(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.RedrawAll Views()
End If
End If

End Sub

End Module
Nov 20 '05 #1
2 6849
"Russ Green" <rg*******@SP AM-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(oArrayLis t.ToArray(GetTy pe(TagElement)) , TagElement()). I get
the follwing error:-

An unhandled exception of type 'System.Invalid CastException' 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(oArrayLis t.ToArray(GetTy pe(TagElement)) , TagElement())
try
dim a as array
a = oArrayList.ToAr ray(GetType(Tag Element))

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*******@free net.de> wrote in message
news:40******** *************** @news.freenet.d e...
"Russ Green" <rg*******@SP AM-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(oArrayLis t.ToArray(GetTy pe(TagElement)) , TagElement()). I get
the follwing error:-

An unhandled exception of type 'System.Invalid CastException' 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(oArrayLis t.ToArray(GetTy pe(TagElement)) , TagElement())
try
dim a as array
a = oArrayList.ToAr ray(GetType(Tag Element))

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
4613
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 project I work on uses Xml serialization to store game objects for loading. One of the techniques we use is to make an array property and use the set...
7
14254
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 the elements will move up one position. This will be called a lot, so looking for speed. This just saving the index 0 as tmp and then enum the list...
9
5081
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 code below): (a)When adding BitArrays to the ArrayList and then looping through the ArrayList I seem to access only the latest added BitArray and I'm...
3
9683
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 atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
6
326
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 arraylist named filterSet. The intended purpose is for this arrayList to contain type mailFilterSet elements. The mailFilterSet class contains a string...
4
2806
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 that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been...
20
5954
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 arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim...
5
6904
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 Preserve SomeArray(c) SomeArray(c) = SomeObject <end loop>
18
3230
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 storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
48
4425
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 lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should...
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.