VB.NET中将变量声明为Byte的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5048562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems in declaring a variable as Byte in VB.NET
提问by SpongeBob SquarePants
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?
你能向我解释为什么会这样吗?
回答by dbasnett
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.
推理通常是一个坏主意。
回答by user541686
You need curly braces, because if you don't put them, it means you're trying to call a constructor for a singleobject -- which is an error for different reasons:
你需要花括号,因为如果你不放它们,这意味着你正在尝试为单个对象调用构造函数——由于不同的原因,这是一个错误:
- You can't assign a single object to an array. (This is always true.)
Bytedoesn't have a constructor. (This is only true in this particular case.)
- 您不能将单个对象分配给数组。(这总是正确的。)
Byte没有构造函数。(这仅在这种特殊情况下才是正确的。)

