473,385 Members | 1,531 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,385 software developers and data experts.

Redim array parameter when using reflection

I have not been able to find the solution of this problem anywhere:

I am building a class PrivateHelper that provides methods to access
private members and invoke private methods, to be used for testing.

In a particular test case, an array is passed to the Method Under Test,
and then that MUT performs a ReDim on the array. The test code then
needs to check the array, but the effect of the ReDim statement does
not propagate to the test code. As long as no ReDim is done, there are
no problems and all changes to the array are visible for the test code.

This is a simplified sketch of how I have done (The PrivateHelper class
is left out):

Sub testCase()
Dim testArray = New Byte() {1, 1}
'setup methodInfo for method "changeArray" in class myObject
methodInfo.Invoke(myObject, New Object() {testArray})
'check if testArray(0) = 2 THIS CHECK FAILS
End Sub

Class myObject
Sub changeArray(ByRef testArray As Byte())
ReDim testArray(3)
testArray(0) = 2
End Sub
End Class

I am not allowed to do any changes in the myObject class. Is there a
way around this problem?

Nov 23 '05 #1
2 2050
"Fredrik Strandberg" <be***********@hotmail.com> schrieb
I have not been able to find the solution of this problem anywhere:

I am building a class PrivateHelper that provides methods to access
private members and invoke private methods, to be used for testing.

In a particular test case, an array is passed to the Method Under
Test, and then that MUT performs a ReDim on the array. The test code
then needs to check the array, but the effect of the ReDim statement
does not propagate to the test code. As long as no ReDim is done,
there are no problems and all changes to the array are visible for
the test code.

This is a simplified sketch of how I have done (The PrivateHelper
class is left out):

Sub testCase()
Dim testArray = New Byte() {1, 1}
'setup methodInfo for method "changeArray" in class myObject
methodInfo.Invoke(myObject, New Object() {testArray})
'check if testArray(0) = 2 THIS CHECK FAILS
End Sub

Class myObject
Sub changeArray(ByRef testArray As Byte())
ReDim testArray(3)
testArray(0) = 2
End Sub
End Class

I am not allowed to do any changes in the myObject class. Is there a
way around this problem?


Sub testCase()
Dim testArray = New Byte() {1, 1}
'setup methodInfo for method "changeArray" in class myObject

dim params as object()
params = new object(){testarray}
methodInfo.Invoke(myObject, New Object() {testArray})
testarray = directcast(params(0), byte())
End Sub
Before, the new Array has been returned to the object array which is the
container for the parameters. The problem was that you didn't store a
reference to the object array, thus you weren't able to access the new
values (the reference to the new array created by Redim) in the
object array after Invoke returned.
Armin

Nov 23 '05 #2
Thanks a lot, solved all my problems! Splendid!

Nov 23 '05 #3

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

Similar topics

2
by: Wayne Wengert | last post by:
I am trying to add one column to an existing array (code below). The ReDim command gives the error: ----------------------------------------------- Microsoft VBScript runtime error '800a0009' ...
2
by: | last post by:
Is it correct to think that after reducing the populated array's size from say, 10 to 5 with redim preserve myArray(i) an attempt to access an element above the fifth does not cause a...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
6
by: Adrian | last post by:
Hi In VB 6 I would declare an array in the general part to make it visible to all parts then once I know how elements I had I would redim it with the amount thus Dim testarray() as string ...
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...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
9
by: Anil Gupte | last post by:
I am having a problem using Multidim arrays. I want to create an array which as I understand it is dimensioned as: dim xyz (rows,columns) as String I want to populate it with rows from a...
1
by: Freddy Coal | last post by:
Hi, I don't know how redim an array, My problem whit an example: I define my array Dim Ary as array I put three elements inside my array Ary = Split("one,two,three", ",")
2
by: eBob.com | last post by:
I was changing some code in a multi-threaded application today and noticed that it was not locking where it really needed to be locking. The Sub was already working with an array so I just stuck a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.