PC Review


Reply
Thread Tools Rate Thread

Array Declaration Syntax

 
 
Jerry Camel
Guest
Posts: n/a
 
      4th Mar 2004
VB seems to accept both versions of this declaration... Are they
equivalent?

Dim Bytes() As Byte = New Byte(Part1.Length + Part2.Length) {} 'Without the
braces, vb complains that Byte has no constructor.
Dim Bytes(Part1.Length + Part2.Length) As Byte

Thanks,
Jerry


 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      4th Mar 2004
Jerry,

>Are they equivalent?


Yes



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      4th Mar 2004
Jerry,
In addition to Mattias's comments.

Just be warned, that your Bytes array will be "off by one" then the total of
your Part1 & Part2.

Remember that when you define an Array the Upper bound is given, not length.
The lower bound is always Zero.

If you want Bytes to contain the same number of elements as Part1 & Part2
you need to subtract one from the total of their lengths...

Something like:
> Dim Bytes(Part1.Length + Part2.Length -1 ) As Byte


For example:
Dim part1(10), part2(10) As Byte
Dim Bytes(Part1.Length + Part2.Length) As Byte

Part1 & Part2 will each have 11 elements, while Bytes will have 23 elements.

Hope this helps
Jay

"Jerry Camel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> VB seems to accept both versions of this declaration... Are they
> equivalent?
>
> Dim Bytes() As Byte = New Byte(Part1.Length + Part2.Length) {} 'Without

the
> braces, vb complains that Byte has no constructor.
> Dim Bytes(Part1.Length + Part2.Length) As Byte
>
> Thanks,
> Jerry
>
>



 
Reply With Quote
 
Jerry Camel
Guest
Posts: n/a
 
      4th Mar 2004
Thank you gentlemen...

Regarding the size issue (And I'm talking about array sizes...) This
question came up whe I was converting some code from C# to VB .NET. Is
there any reason you wouldn't run into the same issue in C#? (I'm just
wondering why the guy that wrote the C# didn't account for this.)

Thanks.

Jerry


"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jerry,
> In addition to Mattias's comments.
>
> Just be warned, that your Bytes array will be "off by one" then the total

of
> your Part1 & Part2.
>
> Remember that when you define an Array the Upper bound is given, not

length.
> The lower bound is always Zero.
>
> If you want Bytes to contain the same number of elements as Part1 & Part2
> you need to subtract one from the total of their lengths...
>
> Something like:
> > Dim Bytes(Part1.Length + Part2.Length -1 ) As Byte

>
> For example:
> Dim part1(10), part2(10) As Byte
> Dim Bytes(Part1.Length + Part2.Length) As Byte
>
> Part1 & Part2 will each have 11 elements, while Bytes will have 23

elements.
>
> Hope this helps
> Jay
>
> "Jerry Camel" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > VB seems to accept both versions of this declaration... Are they
> > equivalent?
> >
> > Dim Bytes() As Byte = New Byte(Part1.Length + Part2.Length) {} 'Without

> the
> > braces, vb complains that Byte has no constructor.
> > Dim Bytes(Part1.Length + Part2.Length) As Byte
> >
> > Thanks,
> > Jerry
> >
> >

>
>



 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      4th Mar 2004
Jerry,

>Is there any reason you wouldn't run into the same issue in C#?


Yes, in C# (and other C style languages) the number inside the square
brackets indicate the array length. So new int[10] gives you an array
of 10 elements, index 0-9.

In VB it works like Jay said, the number you provide specifies the
upper bound. So New Integer(10) {} has 11 elements, index 0-10.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaration of array pramod Microsoft Excel Programming 0 18th Feb 2009 11:06 AM
Looking For Explanation Regarding Declaration Syntax =?Utf-8?B?VGVycmFuY2U=?= Microsoft Dot NET 20 15th Jun 2006 08:31 PM
VB 2005 variable declaration syntax Mark Berry Microsoft VB .NET 3 31st Mar 2006 07:34 PM
Array Declaration mikey10 Microsoft Excel Programming 2 18th Oct 2004 08:19 PM
Array Declaration WFB Microsoft VB .NET 1 30th Jan 2004 03:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:32 PM.