Array declaration VB v C#
Would I be correct in saying that to declare an array of 10 items (for example)
in VB: MyArray(9) Ten items, index 0 to 9
in C# MyArray[10] Ten items, index 0 to 9 (is this right??)
Why is there this difference in the declaration of the bounds of the array? Surely the underlying structure is the same for both when compiled to MSIL?
Just curious!
Re: Array declaration VB v C#
I don't know exactly what factors drove the decisions for each language but don't forget that VB.NET and C# are two different languages developed by two different teams. They were both developed with support for the features of the .NET Framework in mind, but they weren't developed to be the same. They both support the features of .NET arrays and that's all they are required to do.
I'm going to guess that the fact that VB.NET requires you to specify the upper bound rather than the length of the array has something to do with the fact that, prior to .NET, VB arrays were 1-based. I would guess that the shift in start index of arrays by one position has something to do with the shift in the value you use to create the array by one position.
Re: Array declaration VB v C#
In VB 6 (prehistoric days lol) arrays were zero based unless the declaration was specified "Option Base 1" as then it would become one based. Yes you could have also explicitly declared "Option Base 0".
Re: Array declaration VB v C#
Quote:
Originally Posted by RobDog888
In VB 6 (prehistoric days lol) arrays were zero based unless the declaration was specified "Option Base 1" as then it would become one based. Yes you could have also explicitly declared "Option Base 0".
That I didn't know. In that case I have no legitimate theory as to why the VB.NET team made that choice. It's all academic though. What is is. In VB.NET you create an array by specifying the upper bound.
Re: Array declaration VB v C#
Well you did that in VB 6 too but isnt there something other then specifying the lower bound of the array in vb.net to mean start at 1 or 0?
I didnt realize that either about C# and vb.net array difference:
C# - array[9] = 9 elements ?
VB.NET array(9) 10 elements
Re: Array declaration VB v C#
The only way (that I'm aware of) to create arrays with a lower bound other than zero is with the Array.CreateInstance method, which is not limited to VB.NET.
Re: Array declaration VB v C#
Fair point about being two separate languages developed by two separate teams. I supposed I assumed (wrongly) that because they are both .NET, they would share commonality aside from the obvious one of targeting the framework.
The requisite for each language is to be able to target the framework, and how it achieves this is largely immaterial I guess.
How are arrays created in C/C++? Do they require the number of items, or the upper bound?
I'm guessing that VB.NET, for whatever reason, will be the odd one out.
Thanks for the responses, very interesting. :thumb:
Re: Array declaration VB v C#
Quote:
Originally Posted by Andy_P
How are arrays created in C/C++? Do they require the number of items, or the upper bound?
Number of items.
Re: Array declaration VB v C#
I figured as much.
Presumably the team responsible for designing VB.NET in the first place programmed it with C++ (my guess.)
That would mean they made a conscious decision to change the way arrays were handled with respect to the language they were actually using. I wonder why they did that? Given that VB.NET was enough of a difference to VB6, the rules could have been made the same across the .NET platform.
Bit strange in my opinion, for what it's worth.
Re: Array declaration VB v C#
It's just a BASIC tradition to declare arrays that way.
Why should the language used to write a compiler affect the design of the language being compiled?
Re: Array declaration VB v C#
Quote:
Originally Posted by penagate
Why should the language used to write a compiler affect the design of the language being compiled?
Of course, that's correct, no reason at all.
Point I was trying to make is that VB.NET was enough of a break with 'the old ways' of doing things, it could have been an opportunity to bring it in line with other languages. That is probably very naiive of me, as I am sure there are tons of other differences across the platforms.
Traditions are hard to break, and if that's the way it's always been done, then who am I to argue!