6

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?

2 Answers 2

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

accepted answer answers OP's question "Can you explain to me why this is happening?" This answer shows examples how to do it right. This is what I needed thanks.
8

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.)

20 Comments

When you said As Byte() instead of As Byte.
@abcdefghijklmnopqrstuvwxyz: Haha okay... if you're using Visual Basic 2008 or later, then you can use type inference and omit the As Byte() part entirely -- that way, the variable will have the same type as whatever you assign it.
Ugh, I just removed my up-vote because you recommended type inference in VB.NET. That's a very bad idea. It doesn't work exactly the same way as var in C#. Just learn how to declare variables and what the appropriate syntax is. It's not that hard. If you don't know the difference between an array and an object, you're going to have a lot of problems anyway. No reason to create additional ones by programming with Option Infer On.
@Cody: I also compile C++ at level 4, and I also turn all on warnings. And I see your point about how this isn't a good thing for a beginner to use. But that's a different question -- my question was, how is Dim different from var, since you mentioned those are different?
@Mehrdad: The most important difference is that Dim has multiple meanings in VB. If you don't set your project settings exactly the right way, you'll end up where everything is declared as an Object. You need to have Option Strict On and Option Infer Off. The article linked to by the OP doesn't make this clear, and in fact almost so much as encourages you to compile with Option Strict Off. You can't break things nearly this badly with var.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.