views:

542

answers:

6

Hello people, I am trying to convet the following code from C# to Vb using 3.5 framework.

Here is the code in C# that I am having trouble with.

MethodInfo mi = typeof(Page).GetMethod("LoadControl", new Type[2] { typeof(Type), typeof(object[]) });

I thought it would be like this in VB;

Dim mi As MethodInfo = GetType(Page).GetMethod("LoadControl", New Type(2) {GetType(Type), GetType(Object())})

but I am getting the following error "array initializer is missing 1 elements"

The other line that I am having trouble with and getting the same error is

control = (Control) mi.Invoke(this.Page, new object[2] { ucType, null });

I tried this in vb but it does not work.

control = DirectCast(mi.Invoke(Me.Page, New Object(2) {ucType, Nothing}), Control)

ucType is defined as follows

Dim ucType As Type = Type.[GetType](typeName(1), True, True)

Any help would be greatly appreciated.

Thanks Joe Doc

+1  A: 

It will be like this -

Dim mi As MethodInfo = GetType(Page).GetMethod("LoadControl", New Type(1) {GetType(Type), GetType(Object())})

The other one will be -

control = DirectCast(mi.Invoke(Me.Page, New Object(1) {ucType, Nothing}), Control)
Kirtan
ThanksCan't believe I missed that.I really appreciate your help.
+2  A: 

For the first line you need to change new Type(2) into New Type(1).

Dim mi As MethodInfo = GetType(Page).GetMethod("LoadControl", New Type(1) {GetType(Type), GetType(Object())})

In VB.Net the number specified in the array initializer is the highest accessible index vs. the length. The second line you mentioned has the same problem and solution.

JaredPar
+5  A: 

VB.Net arrays are 0-based, but declared using the highest-index rather than the number of items. So a 10-item array, indexed 0..9, is declared as Item(9).

With that said, the real solution to your problem is to let the compiler figure out the array length, like so:

Dim mi As MethodInfo = GetType(Page).GetMethod("LoadControl", New Type() {GetType(Type), GetType(Object())})
Iceman
+1 for a good answer, explanation, and best-solution
STW
ThanksCan't believe I missed that.I really appreciate your help.
+3  A: 

In VB.NET the array declaration takes the upper bound of the array, not the length like C# does (kind of silly if you ask me). Because of this, you need to reduce the number passed into your array declarations by 1 (since arrays are zero-based).

Adam Robinson
+2  A: 

VB uses the upper bound as the argument for arrays.

new byte[X]

new byte(X) 'wrong, 1 more element
new byte(X-1) 'correct, kinda confusing
new byte(0 to X-1) 'correct, less confusing

I suggest using the (0 to X-1) style, because it's a lot clearer. It was a lot more important in the vb6 days when array(X) could mean 0 to X or 1 to X depending on the context.

Strilanc
+1  A: 

hi. There is an online version available at http://converter.telerik.com which converts C# to VB and vice versa.

convertors are always handy, but they don't teach you much or tell you *why* a change is made. I hope I don't see you spreading this link everywhere :-P
STW
Also it gets this line of code wrong.
jleedev