Connecting Tech Pros Worldwide Forums | Help | Site Map

Help deciphering this line of code...

Terry Olsen
Guest
 
Posts: n/a
#1: Nov 21 '05
Dim bytes() As Byte = New Byte(1023) {}

If someone could help me understand this line of code (especially the part
to the right of the equal sign) I'd be much obliged...



alxdotnet
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Help deciphering this line of code...


It creates an array named bytes and initializes it with 1024(indicies 0-1023)
byte structures.

Piece by piece:

Dim bytes() - Dimension the variable bytes as an array
As Byte - of Byte structures
= New Byte(1023) {} - and initialize it to a newly created array of 1023
Bytes. Basically, the {} tells VB that this is a New array we are creating,
not a single instance of Byte. (1023) means that this new array has 1024
elements.

"Terry Olsen" wrote:
[color=blue]
> Dim bytes() As Byte = New Byte(1023) {}
>
> If someone could help me understand this line of code (especially the part
> to the right of the equal sign) I'd be much obliged...
>
>
>[/color]
Dennis
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Help deciphering this line of code...


You can also initialize the array values as follows:

Dim bytes As Byte() = New Byte() {1,2,3,4,5,6,7,8,9,10}

This creates a byte array from index of 0 to 9 where the values are:

bytes(0) = 1
bytes(1) = 2
.......
bytes(9) = 10

Note when doing this, you can't specifiy the size of the array like (10)
since the size of the array is controlled by the number if values between
the {}

"Terry Olsen" wrote:
[color=blue]
> Dim bytes() As Byte = New Byte(1023) {}
>
> If someone could help me understand this line of code (especially the part
> to the right of the equal sign) I'd be much obliged...
>
>
>[/color]
Cor Ligthert
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Help deciphering this line of code...


Terry,

In addition to the others, probably written from someone who converted C# to
VBNet.
[color=blue]
> Dim bytes() As Byte = New Byte(1023) {}
>[/color]

Dim bytes(1023) as Byte

As we are used to do it.

Cor



One Handed Man \( OHM - Terry Burns \)
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Help deciphering this line of code...


the {} is an initialiser, the word New is what tells VB that this is a new
instance of a Type

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"alxdotnet" <alxdotnet@discussions.microsoft.com> wrote in message
news:D4B80626-D74B-4618-AF54-779AEB44E64A@microsoft.com...[color=blue]
> It creates an array named bytes and initializes it with 1024(indicies[/color]
0-1023)[color=blue]
> byte structures.
>
> Piece by piece:
>
> Dim bytes() - Dimension the variable bytes as an array
> As Byte - of Byte structures
> = New Byte(1023) {} - and initialize it to a newly created array of 1023
> Bytes. Basically, the {} tells VB that this is a New array we are[/color]
creating,[color=blue]
> not a single instance of Byte. (1023) means that this new array has 1024
> elements.
>
> "Terry Olsen" wrote:
>[color=green]
> > Dim bytes() As Byte = New Byte(1023) {}
> >
> > If someone could help me understand this line of code (especially the[/color][/color]
part[color=blue][color=green]
> > to the right of the equal sign) I'd be much obliged...
> >
> >
> >[/color][/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Nov 21 '05

re: Help deciphering this line of code...


* =?Utf-8?B?YWx4ZG90bmV0?= <alxdotnet@discussions.microsoft.com> scripsit:[color=blue]
> It creates an array named bytes and initializes it with 1024(indicies 0-1023)
> byte structures.
>
> Piece by piece:
>
> Dim bytes() - Dimension the variable bytes as an array
> As Byte - of Byte structures
> = New Byte(1023) {} - and initialize it to a newly created array of 1023
> Bytes. Basically, the {} tells VB that this is a New array we are creating,
> not a single instance of Byte.[/color]

The '{}' tells VB that it should create an array and don't call a
parameterized constructor of the class/structure. It must be specified
to remove ambiguity.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Terry Olsen
Guest
 
Posts: n/a
#7: Nov 21 '05

re: Help deciphering this line of code...


> In addition to the others, probably written from someone who converted C#[color=blue]
> to
> VBNet.
>[color=green]
>> Dim bytes() As Byte = New Byte(1023) {}[/color]
>
> Dim bytes(1023) as Byte
>
> As we are used to do it.[/color]

That's why I was confused. I looked at that and wondered why we couldn't
just do it the VB way. I thought maybe it created something completely
different.


Closed Thread