2

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.

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I believe this is the answer I am looking for since there isn't anything in the curly brackets right after New Object(0 - 1). Thanks for the help!
I don't think that having nothing in the curly brackets is particularly relevant to it being a zero-length array. e.g. in VB.NET, New Object(4) {} would give you a 5 element array containing nulls, since no initial values are provided. The {} is just required in VB.NET, even when not initializing the array, while it's optional in C#. This also shows the possible reason for the style used by the original programmer - New Object(5 - 1) {} could be a reminder that VB.NET is a bit odd in how the array dimensions are declared, vs. New Object(4) {} - both are 5 element arrays!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.