Results 1 to 11 of 11

Thread: Array declaration VB v C#

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  9. #9

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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?

  11. #11

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width