passing contents between arrays of different types

G

Guest

is there a way to pass the contents of one array to another array of different types, without looping
looping 1000 times is too long, is there a method that can do it in one step

for example..
dim array1() as strin
dim array2() as byt
redim array1(1000
redim array2(1000

''' populate the arrays

for i as int16=0 to 99
array2(i) = cbyte(array1(i)
nex
 
B

Bernie Yaeger

Hi Howie,

It won't work with regular arrays because arrays store data in memory based
on the datatype, so it can't readily alter the space allocated for, say, an
integer and convert it to a string.

However, you can use a clone method with arraylists that are different, as
arraylists are essentially data type agnostic. Here's an example:
Dim arrayints(2) As Integer

Dim arraystrings(2) As String

arrayints(0) = 23

arrayints(1) = 30

arrayints(2) = 15

'arraystrings = arrayints.Clone

'**** above line fails

Dim i As Integer

'For i = 0 To 2

'MessageBox.Show(arraystrings(i))

'Next

Dim arraylistints As New ArrayList

Dim arrayliststrings As New ArrayList

arraylistints.Add(26)

arraylistints.Add(11)

arraylistints.Add(459)

arrayliststrings = arraylistints.Clone

'**** above line works fine

For i = 0 To 2

MessageBox.Show(arrayliststrings(i))

Next

HTH,

Bernie Yaeger

howie said:
is there a way to pass the contents of one array to another array of
different types, without looping.
 
G

Guest

Hi Bernie, I tested the code...

I use option strict ON, so it fails at
'\\\arrayliststring = arraylistints.clone
'//// how do you overcome this?

another question... the types for arrayliststring and arraylistints are not defined. (as string, as integer??)
was it left out intentionally? cos if my target array is arraylistints and it has
to be an array of integers, how can it be done?
 
B

Bernie Yaeger

Hi Howie,

My system had no trouble with the code I sent you. I'm using vb .net 2003
and option strict is on by default, so I'm curious as to why your system
chocked on it. Try it again and let me know what error you get and which
version of visual studio you're running.

Also, no, arraylists are not defined as to type - that's the point with
arraylists: they can contain any data.

HTH,

Bernie

howie said:
Hi Bernie, I tested the code...

I use option strict ON, so it fails at
'\\\arrayliststring = arraylistints.clone
'//// how do you overcome this?

another question... the types for arrayliststring and arraylistints are
not defined. (as string, as integer??)
 
J

Jay B. Harlow [MVP - Outlook]

Bernie,
Huh?
Dim arraylistints As New ArrayList
Dim arrayliststrings As New ArrayList

arraylistints.Add(26)

arrayliststrings = arraylistints.Clone
'**** above line works fine

The above line FAILS as arraylistints.Clone returns an Object, you cannot
assign an Object to an Arraylist with Option Strict On with out using
DirectCast! I normally put Option Strict On at the top of each file, so I
know it is really on!

This line WILL work with Option Strict On!
arrayliststrings = DirectCast(arraylistints.Clone, ArrayList)
'**** above line works fine
MessageBox.Show(arrayliststrings(i))
The above line FAILS as arrayliststrings(i) returns an Object,
MessageBox.Show expects a String,

If arrayliststrings contained Strings as Howie wanted, he could use
DirectCast, unfortunately it does not as you used Clone above, which copies
the boxed integers to boxed integers. Using CType will work as CType is
converting the boxed integer to a String. Howie did not want to have to call
CType explicitly.

Note DirectCast will cast an object, to the type it really is, while CType
will attempt to convert the object to the type you are asking.

Unfortunately I do not see a way that Howie can "have his cake & eat it to",
Other then Howie's loop I know of no built-in ways of converting an array of
one type to an array of another type! I'm not sure if one could use
System.Convert.ChangeType in a generalized routine to convert an array of
one type to an array of another type.

Hope this helps
Jay
 

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