C#之后的“ New SomeClass {Key .SomeProperty = SomeValue}”中的语法错误。 VB转换 [英] Syntax error in `New SomeClass { Key .SomeProperty = SomeValue }` after C# -> VB conversion
问题描述
我和我的一位同事都在编程。他用C#上了一堂课,我正在将其转换为VB.NET。我得到了完整的类的转换,除了一行以外,这时我无法弄清楚,所以以为新的眼睛也许可以找到我的错误。
原始C#代码
使用(var client = new HttpClient(new HttpClientHandler {AutomaticDecompression = DecompressionMethods.GZip | | DecompressionMethods.Deflate}))
已转换的VB.NET代码
使用客户端=新HttpClient(新的HttpClientHandler与{Key .AutomaticDecompression = DecompressionMethods.GZip或DecompressionMethods.Deflate })
错误
字段名称或在对象初始化中初始化的属性必须以。开头。
错误位于键下
最后的注意::我大部分使用了可怕的代码转换器,所以我不确定键在哪里来自。
有两个概念具有相似的语法但语义不同:
匿名类型
C#:新{A = 1,B = 2}
VB:新增了{键.A = 1,键.B = 2}
在这里, VB 也允许您添加可变(非关键)属性,而 C#不支持:
新增了{键.A = 1,键.B = 2,.SomeMutableProperty = 3}
因此,键
关键字在这里很重要。
命名类型的对象初始化程序
C#: new MyClass {A = 1,B = 2}
VB: New MyClass With {.A = 1,.B = 2}
此处, MyClass的现有属性设置,因此 Key
关键字是不相关的,因此是不允许的。
显然,您的C#-> VB转换器认为这是一个匿名类型,尽管它是对象初始化程序。删除 Key
关键字,并将错误报告发送给转换器的开发人员。
A co-worker of mine and I both do programming. He has made a class in C# and I am working on converting it to VB.NET. I got the full class converted except for a single line, and at this point I cannot figure it out so thought a fresh set of eyes maybe able to find my error.
Original C# code
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
Converted VB.NET code
Using client = New HttpClient(New HttpClientHandler With {Key .AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate})
Error Name of field or Property being initialized in an object initialize must start with '.'.
Error is located under the 'Key'
Last note: I used a dreaded code-converter for most of it, so I am unsure where 'key' came from.
There are two concepts which have similar syntax but different semantics:
Anonymous Types
C#: new { A = 1, B = 2 }
VB: New With { Key .A = 1, Key .B = 2 }
Here, VB also allows you to add mutable (non-key) properties, which C# does not support:
New With { Key .A = 1, Key .B = 2, .SomeMutableProperty = 3 }
Hence, the Key
keyword is important here.
Object Initializers for Named Types
C#: new MyClass { A = 1, B = 2 }
VB: New MyClass With { .A = 1, .B = 2 }
Here, existing properties of MyClass are set, so the Key
keyword is irrelevant, and, thus, not allowed.
Apparently, your C# -> VB converter thought that this was an anonymous type, although it was an object initializer. Remove the Key
keyword and send a bug report to the converter's developer.
这篇关于C#之后的“ New SomeClass {Key .SomeProperty = SomeValue}”中的语法错误。 VB转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!