Jared.Rodgers
Jared.Rodgers

Reputation: 141

Converting VB.Net Array to C#

Here is a piece of VB.Net code:

Dim left As Object = NewLateBinding.LateGet(NewLateBinding.LateGet(sender, Nothing, "PressedLink", New Object(0 - 1) {}, Nothing, Nothing, Nothing), Nothing, "ItemName", New Object(0 - 1) {}, Nothing, Nothing, Nothing)

My question is with the:

New Object(0 - 1)

The online converter converted it to the following C# code:

New Object[0 - 1]

I did some research on the VB.Net array code and it look like the (0 - 1) is just telling which numbers the index starts and ends on. However, in C# the compiler sees this as a negative 1. I have a feeling that in C# it should be:

New Object[2]

I just wanted to see if anyone who is more familiar with VB.Net can verify this for me so I know I am doing it correctly or not.

Upvotes: 2

Views: 746

Answers (1)

Mark
Mark

Reputation: 8150

In VB.NET, when declaring an array you specify the maximum index for each dimension, and these are zero based. So, New Object(0) {} is actually an array with one item. Because of this, to declare an array with no items, you use New Object(-1) {}. See here for more details.

In your VB.NET code, New Object(0 - 1) is the same as New Object(-1) - the 0 - 1 part is just zero minus one, or -1, meaning a zero-length array of Object.

The C# equivalent is just new Object[0], since in C# you specify the number of elements when declaring an array.

Upvotes: 4

Related Questions