|
-
Apr 13th, 2007, 04:51 PM
#1
Thread Starter
Fanatic Member
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!
-
Apr 13th, 2007, 07:58 PM
#2
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.
-
Apr 14th, 2007, 02:46 AM
#3
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".
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 14th, 2007, 03:27 AM
#4
Re: Array declaration VB v C#
 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.
-
Apr 14th, 2007, 03:47 AM
#5
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 14th, 2007, 04:09 AM
#6
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.
-
Apr 14th, 2007, 04:28 AM
#7
Thread Starter
Fanatic Member
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.
-
Apr 14th, 2007, 04:44 AM
#8
Re: Array declaration VB v C#
 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.
-
Apr 14th, 2007, 04:58 AM
#9
Thread Starter
Fanatic Member
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.
-
Apr 14th, 2007, 05:00 AM
#10
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?
-
Apr 14th, 2007, 05:09 AM
#11
Thread Starter
Fanatic Member
Re: Array declaration VB v C#
 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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|