Solved

VB6 Variant (Array) to VB.NET Array

Posted on 2004-09-29
5
1,381 Views
Last Modified: 2008-02-01
Hi,

I have a VB6 method that returns an array of strings as a Variant.

When I try and convert this to a VB.NET string array, I receive the cast error. So, to get around this I do the following:

Dim a As Array = CTYPE(old.Method(), Array)

This does not throw an exception, so I therefore assume that the VB6 method is indeed returning an array object. However, I cannot cast this to a string array, even though each element in the Array 'a' is of type string.

Also,

Dim obj() As Object = CTYPE(old.Method(), Object()) produces the same error.

So, I cannot cast the returned Variant to an Object() / String(), but I can cast it to Array.

Furthermore, in VB.NET I would simply do,

Private Function a() As Object
  Dim str(1) As String
  Return str
End Function

Dim newStr() As String = CTYPE(a(), String())

Thanks.
0
Comment
Question by:robert_83
5 Comments
 
LVL 18

Accepted Solution

by:
armoghan earned 100 total points
ID: 12179294
Instead of making String array make object array
0
 
LVL 3

Assisted Solution

by:Arundel_Castle
Arundel_Castle earned 100 total points
ID: 12179558
Try it without an explict cast. Then cast the strings beneath it. My resoning is that object() has no knowledge of variant and you have to cast .Net types to object. The implict cast might trigger something else.

Dim obj() As Object

obj = old.Method()
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question