473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Array Declaration Syntax

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
Nov 20 '05 #1
4 1227
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.
Nov 20 '05 #2
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" <rl*****@msn.com> wrote in message
news:eO**************@tk2msftngp13.phx.gbl... 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

Nov 20 '05 #3
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]" <Ja************@msn.com> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
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" <rl*****@msn.com> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
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


Nov 20 '05 #4
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.
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
5
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as...
11
by: Magix | last post by:
Hi, what is wrong with following code ? typedef struct { word payLen; datatype data; } msg_type;
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
4
by: Alan Foxmore | last post by:
Hi everyone, I'm new to C# and I was hoping I could get some clarification on the syntax for jagged and multidimensional arrays. Here is my question: The following syntax is correct for...
4
by: werasm | last post by:
Hi all, I have recently come accross this function declaration syntax. Initially it was puzzling (still is, to be honest), but I now realize that it declares a function that returns a reference...
5
by: latin & geek via DotNetMonster.com | last post by:
dear all, hi. i have a little sub routine which works fine in my dummy test program, but when i insert it in my main program it throws errors. this is the subroutine: Public Sub...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
18
by: mdh | last post by:
>From p112 ( K&R). Given an array declared as static char arr= { { 0,1,........},{0,1,.....}}; let arr be passed as an argument to f. f( int (*arr) ) {....} It is noted that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.