无法初始化十六进制数数组:"Keyword does not name a type"

Failed to initialize an array of hexadecimal numbers: "Keyword does not name a type"

我正在尝试像这样初始化十六进制数字数组:

Module VBModule
    Sub Main()
        Console.WriteLine("Hello World")
        Dim someArray As Variant
        someArray = Array(&H11, &H22, &H33)
        
    End Sub
End Module

我正在使用 this 网站 运行 它并且我收到错误:

Visual Basic.Net Compiler version 0.0.0.5943 (Mono 4.6 - tarball)
Copyright (C) 2004-2010 Rolf Bjarne Kvinge. All rights reserved.
/home/main.vb (11,29) : error VBNC30180: Keyword does not name a type.
/home/main.vb (12,14): Compiler error around this location, the compiler hasn't implemented the error message, nor error recovery, so the compiler will probably crash soon.
  at vbnc.Helper.ErrorRecoveryNotImplemented (vbnc.Span Location) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseVariableDeclarator (vbnc.ParsedObject Parent, vbnc.Modifiers Modifiers, vbnc.ParseAttributableInfo Info, System.Collections.IList result, System.Boolean local) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseLocalVariableDeclarator (vbnc.ParsedObject Parent, vbnc.Modifiers Modifiers, vbnc.ParseAttributableInfo Info) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseLocalVariableDeclarators (vbnc.ParsedObject Parent, vbnc.Modifiers Modifiers, vbnc.ParseAttributableInfo Info) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseLocalDeclarationStatement (vbnc.CodeBlock Parent) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseCodeBlock (vbnc.ParsedObject Parent, System.Boolean IsOneLiner) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseSubDeclaration (vbnc.TypeDeclaration Parent, vbnc.ParseAttributableInfo Info) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseTypeMembers (vbnc.TypeDeclaration Parent) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseModuleDeclaration (vbnc.ParsedObject Parent, vbnc.Attributes Attributes, System.String Namespace) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseTypeDeclaration (vbnc.ParsedObject Parent, vbnc.Attributes Attributes, System.String Namespace) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseAssemblyMembers (vbnc.AssemblyDeclaration Parent, System.String RootNamespace) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.ParseAssemblyDeclaration (System.String RootNamespace, vbnc.AssemblyDeclaration assembly) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Parser.Parse (System.String RootNamespace, vbnc.AssemblyDeclaration assembly) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Compiler.Compile_Parse () [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Compiler.Compile () [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Compiler.Compile (System.String[] CommandLine) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
  at vbnc.Main.Main (System.String[] CmdArgs) [0x00000] in <cd55155bc3b542a49b0b2bb7e08ded22>:0 
There were 1 errors and 0 warnings.
Compilation took 00:00:02.3128040

我做错了什么?

VB.NET中没有十六进制数本身。十六进制只是表示整型文字值的一种方式,即 ByteShortIntegerLong。如果你想要一个 Integer 值的数组,那么这就是你需要创建的:

Dim someArray As Integer() = {&H11, &H22, &H33}

默认情况下使用 Option Infer On,您也可以允许推断类型:

Dim someArray = {&H11, &H22, &H33}

在这两种情况下,变量现在都指向一个包含值 17、34 和 51 的数组。它们只是数字,而不是十六进制数字。您可以使用十进制或八进制文字来获得完全相同的数组。十六进制数通常最有用的地方是表示 Byte 值,其中两位数字可以表示从 0 到 255 的完整值范围:

Dim someArray As Byte() = {&H11, &H22, &H33}

在那种情况下,您需要明确数组的类型,因为类型推断会产生一个 Integer 数组。要使用类型推断,您必须走更冗长且因此毫无意义的路线:

Dim someArray = {CByte(&H11), CByte(&H22), CByte(&H33)}