[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Is there a way to resize an array from inside a procedure? Using Reflection maybe?

jbrock

11/18/2008 4:29:00 AM

I want to write a VB.NET subroutine that will take an input array
of arbitrary type and resize it. Here is a trivial example of what
I am trying to do:

'Increase the length of an arbitrary input array by one, using ReDim.
Public Sub TryToReDim(ByRef ArrayIn As Object())
ReDim Preserve ArrayIn(ArrayIn.Length)
End Sub

Actually what I really want -- because VB is so braindead when it
comes to array manipulation -- is a Perl style Splice() routine,
which of course would need to be able to resize the input array.
I have a routine like that that works fine in VBA, but when I try
to translate it to VB.NET I am tripped up by casting exceptions.
Here is an example of the problem:

Dim xxx() as String = {"aaa", "bbb", "ccc"}
TryToReDim(xxx)

This throws the following exception:

InvalidCastException occurred
Unable to cast object of type 'System.Object[]' to type 'System.String[]'.

The problem is that, although the input variable ArrayIn is of type
String[], ReDim, instead of redimensioning the array that was
passed, creates a new, local array of type Object[], which then
can't be assigned back to the original String[] input variable.

So what can I do? For example, is there some way that, instead of
using ReDim, I could examine the input array, determine its type,
and create a properly sized local ArrayIn array of the same type,
which could eventually be copied back without problems? I know
that there is a such thing as Reflection in .NET, but it's not
clear to me how that would apply in this case.

For that matter, does VB.NET already have the equivalent of Splice()
somewhere, which, for example, would allow me to insert an element
into the middle of an array, without having to jump though hoops?
I have never understood why it is so awkward to manipulate arrays
in VBA and VB.NET! Does anyone have any idea why, after all this
time, this hasn't been fixed?
--
John Brock
jbrock@panix.com

5 Answers

Michel Posseth [MCP]

11/18/2008 6:04:00 AM

0


> "VB is so braindead when it comes to array manipulation"

Just one question did you ever noticed the array class , in the .Net
Framework ?
imho
VB and C# are masters with array manipulation


http://msdn.microsoft.com/en-us/library/system....

resizing with the array class

http://msdn.microsoft.com/en-us/library/bb3...

and lots and lots more

The exception that you encounter is obvious , you might be bether of with
generics in your case


HTH

Michel Posseth



"John Brock" <jbrock@panix.com> schreef in bericht
news:gftga1$f9a$1@reader1.panix.com...
>I want to write a VB.NET subroutine that will take an input array
> of arbitrary type and resize it. Here is a trivial example of what
> I am trying to do:
>
> 'Increase the length of an arbitrary input array by one, using ReDim.
> Public Sub TryToReDim(ByRef ArrayIn As Object())
> ReDim Preserve ArrayIn(ArrayIn.Length)
> End Sub
>
> Actually what I really want -- because VB is so braindead when it
> comes to array manipulation -- is a Perl style Splice() routine,
> which of course would need to be able to resize the input array.
> I have a routine like that that works fine in VBA, but when I try
> to translate it to VB.NET I am tripped up by casting exceptions.
> Here is an example of the problem:
>
> Dim xxx() as String = {"aaa", "bbb", "ccc"}
> TryToReDim(xxx)
>
> This throws the following exception:
>
> InvalidCastException occurred
> Unable to cast object of type 'System.Object[]' to type 'System.String[]'.
>
> The problem is that, although the input variable ArrayIn is of type
> String[], ReDim, instead of redimensioning the array that was
> passed, creates a new, local array of type Object[], which then
> can't be assigned back to the original String[] input variable.
>
> So what can I do? For example, is there some way that, instead of
> using ReDim, I could examine the input array, determine its type,
> and create a properly sized local ArrayIn array of the same type,
> which could eventually be copied back without problems? I know
> that there is a such thing as Reflection in .NET, but it's not
> clear to me how that would apply in this case.
>
> For that matter, does VB.NET already have the equivalent of Splice()
> somewhere, which, for example, would allow me to insert an element
> into the middle of an array, without having to jump though hoops?
> I have never understood why it is so awkward to manipulate arrays
> in VBA and VB.NET! Does anyone have any idea why, after all this
> time, this hasn't been fixed?
> --
> John Brock
> jbrock@panix.com
>


Armin Zingler

11/18/2008 8:05:00 AM

0

"John Brock" <jbrock@panix.com> schrieb
> I want to write a VB.NET subroutine that will take an input array of
> arbitrary type and resize it.

It is not possible to resize an array. Not in VB.Net, not in any other .Net
Framework based language. Read also the documentation on Redim Preserve. It
creates a new array as you've already pointed out below on your own.

Use one of the many other helpful collections:
http://msdn.microsoft.com/en-us/library/7y3...
(maybe before all "commonly used collections")

> Dim xxx() as String = {"aaa", "bbb", "ccc"}
> TryToReDim(xxx)
>
> This throws the following exception:

No, It can not be compiled. Enable Option Strict On! "It might work" is not
good enough.


Armin

Mike

11/18/2008 12:57:00 PM

0

"John Brock" <jbrock@panix.com> wrote in message
news:gftga1$f9a$1@reader1.panix.com...
> For that matter, does VB.NET already have the equivalent of Splice()
> somewhere, which, for example, would allow me to insert an element
> into the middle of an array, without having to jump though hoops?
> I have never understood why it is so awkward to manipulate arrays
> in VBA and VB.NET! Does anyone have any idea why, after all this
> time, this hasn't been fixed?

The problem is not VB, it's your approach. The array is a fixed size block
of memory as Armin pointed out. You should be using one of the many
collection classes if you want to insert items in the middle of a list. Most
likely the List class is what you need as it has an insert method. The list
class is generic so can be created as type string

Dim list as New List(Of String)
list.add("Aa")
list.add("ba")
list.add("va")
list.add("da")
list.Insert("aaaa", 3")

Michael


PvdG42

11/18/2008 3:10:00 PM

0


"John Brock" <jbrock@panix.com> wrote in message
news:gftga1$f9a$1@reader1.panix.com...
>I want to write a VB.NET subroutine that will take an input array
> of arbitrary type and resize it. Here is a trivial example of what
> I am trying to do:
>
> 'Increase the length of an arbitrary input array by one, using ReDim.
> Public Sub TryToReDim(ByRef ArrayIn As Object())
> ReDim Preserve ArrayIn(ArrayIn.Length)
> End Sub
>
> Actually what I really want -- because VB is so braindead when it
> comes to array manipulation -- is a Perl style Splice() routine,
> which of course would need to be able to resize the input array.
> I have a routine like that that works fine in VBA, but when I try
> to translate it to VB.NET I am tripped up by casting exceptions.
> Here is an example of the problem:
>
> Dim xxx() as String = {"aaa", "bbb", "ccc"}
> TryToReDim(xxx)
>
> This throws the following exception:
>
> InvalidCastException occurred
> Unable to cast object of type 'System.Object[]' to type 'System.String[]'.
>
> The problem is that, although the input variable ArrayIn is of type
> String[], ReDim, instead of redimensioning the array that was
> passed, creates a new, local array of type Object[], which then
> can't be assigned back to the original String[] input variable.
>
> So what can I do? For example, is there some way that, instead of
> using ReDim, I could examine the input array, determine its type,
> and create a properly sized local ArrayIn array of the same type,
> which could eventually be copied back without problems? I know
> that there is a such thing as Reflection in .NET, but it's not
> clear to me how that would apply in this case.
>
> For that matter, does VB.NET already have the equivalent of Splice()
> somewhere, which, for example, would allow me to insert an element
> into the middle of an array, without having to jump though hoops?
> I have never understood why it is so awkward to manipulate arrays
> in VBA and VB.NET! Does anyone have any idea why, after all this
> time, this hasn't been fixed?
> --
> John Brock
> jbrock@panix.com
>
Brain dead? As you've been told, you need exposure to the various
collections classes in .NET.
It's easy to convert your array to a List<>, then do any resizing needed,
then convert the List<> back to an array if you must have it as an array.
Look up the collections classes in MSDN and observe all the management
methods available.

Tom Shelton

11/18/2008 5:22:00 PM

0

On 2008-11-18, John Brock <jbrock@panix.com> wrote:
> I want to write a VB.NET subroutine that will take an input array
> of arbitrary type and resize it. Here is a trivial example of what
> I am trying to do:
>
> 'Increase the length of an arbitrary input array by one, using ReDim.
> Public Sub TryToReDim(ByRef ArrayIn As Object())
> ReDim Preserve ArrayIn(ArrayIn.Length)
> End Sub
>
> Actually what I really want -- because VB is so braindead when it
> comes to array manipulation -- is a Perl style Splice() routine,
> which of course would need to be able to resize the input array.
> I have a routine like that that works fine in VBA, but when I try
> to translate it to VB.NET I am tripped up by casting exceptions.
> Here is an example of the problem:
>
> Dim xxx() as String = {"aaa", "bbb", "ccc"}
> TryToReDim(xxx)
>
> This throws the following exception:
>
> InvalidCastException occurred
> Unable to cast object of type 'System.Object[]' to type 'System.String[]'.
>
> The problem is that, although the input variable ArrayIn is of type
> String[], ReDim, instead of redimensioning the array that was
> passed, creates a new, local array of type Object[], which then
> can't be assigned back to the original String[] input variable.
>
> So what can I do? For example, is there some way that, instead of
> using ReDim, I could examine the input array, determine its type,
> and create a properly sized local ArrayIn array of the same type,
> which could eventually be copied back without problems? I know
> that there is a such thing as Reflection in .NET, but it's not
> clear to me how that would apply in this case.
>
> For that matter, does VB.NET already have the equivalent of Splice()
> somewhere, which, for example, would allow me to insert an element
> into the middle of an array, without having to jump though hoops?
> I have never understood why it is so awkward to manipulate arrays
> in VBA and VB.NET! Does anyone have any idea why, after all this
> time, this hasn't been fixed?

Something like this:

Option Explicit On
Option Strict On

Imports System
Imports System.Collections.Generic

Module Module1

Sub Main()
Dim arr() As String = {"aaa", "ccc"}

Console.WriteLine("==================== Before ======================")
Array.ForEach(arr, AddressOf PrintElement)
Console.WriteLine("==================================================")

arr = Splice(Of String)(arr, "bbb", 1)

Console.WriteLine("===================== After ======================")
Array.ForEach(arr, AddressOf PrintElement)
Console.WriteLine("==================================================")

End Sub

Sub PrintElement(ByVal element As String)
Console.WriteLine(element)
End Sub

Function Splice(Of T)(ByVal arr() As T, ByVal item As T, ByVal index As Integer) As T()
Dim l As New List(Of T)(arr)
l.Insert(index, item)
Return l.ToArray()
End Function
End Module

That implementation of splice, is well very naive - there are no error checks
or anything :) But, as others have already said, I think what you really should
be looking at are Generics and the Generic colleciton classes. They are much
more flexible.

--
Tom Shelton