Problems in declaring a variable as Byte in VB.NET

28,435

Solution 1

You need curly braces, because if you don't put them, it means you're trying to call a constructor for a single object -- which is an error for different reasons:

  1. You can't assign a single object to an array. (This is always true.)
  2. Byte doesn't have a constructor. (This is only true in this particular case.)

Solution 2

Some flavors

    Dim b() As Byte 'b is nothing
    Dim b1(1023) As Byte 'b1 is an array of 1024 elements, all equal to 0
    Dim b2() As Byte = New Byte() {85, 99, 1, 255} 'four elements

    b = New Byte() {} 'zero element array
    b = New Byte() {1, 2} 'two element array

Inference is generally a bad idea.

Share:
28,435
SpongeBob SquarePants
Author by

SpongeBob SquarePants

Top Secret I live Deep down in the Pacific Ocean in the subterranean city of Bikini Bottom in a pineapple along with my pet Gary. I make delicious Krabby Patty Burger at Krusty Krabs. My favorite quote: Isn't this great Squidward? Its just the 3 of us. You, me, and this brick wall you built between us

Updated on July 05, 2022

Comments

  • SpongeBob SquarePants
    SpongeBob SquarePants almost 2 years

    I'm trying out a program which I found on the net. Why is it necessary to put to curly braces at the end of the statement? It gives an error: "Byte has no constructors".

    Dim data As Byte() = New Byte(1023) {}
    

    I can't put the code like this either, it produces the error "byte cannot be a 1-dimensional array".

    Dim arr As Byte() = New Byte()
    

    Can you explain to me why this is happening?