我怎样写[默认值(NULL)在VB.NET? <默认值(没有)GT;不编译 [英] How do I write [DefaultValue(null)] in VB.NET? <DefaultValue(Nothing)> does not compile

查看:138
本文介绍了我怎样写[默认值(NULL)在VB.NET? <默认值(没有)GT;不编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不是琐碎这似乎是。这是一个后续这个问题



让我们说我有一个Windows窗体用户控件的属性为:



<预类=郎-CS prettyprint-覆盖> 使用System.ComponentModel //;

[默认值(NULL)
公共DataSource对象{...}

如果我翻译这VB.NET,我会尝试这样的:



<预类=朗动prettyprint-覆盖> '进口System.ComponentModel

<默认值(没有)GT;
公共财产的DataSource作为对象

端物业

这不会工作,因为编译器选择正确的 DefaultValueAttribute 的构造过载:




重载决策失败,因为没有访问是最具体的这些参数:




  • 的Public Sub New(价值布尔):不是最具体

  • 的Public Sub New(价值字节):不是最具体

  • 的Public Sub New(价值为CHAR):没有最具体




我敢肯定,这是因为没有在VB.NET不仅意味着(在C​​#)的空引用,也有参数类型为默认值(默认(T)在C#)。因为这种模糊的,每一个构造函数重载是一个潜在的匹配



<默认值(CObj(没有))> 也不会工作,因为它不是一个恒定值。



究竟是如何的的我写这篇文章的VB.NET?



PS: <默认值(的GetType(对象),为Nothing)> 是一个选择,但它绕过问题。我如果有任何的方式来使用 DefaultValueAttribute 相同的构造函数的C#版本一样。


解决方案

[...]将无法工作,因为编译器有选择 DefaultValueAttribute 构造函数[。]




编译器可以用没事的类型转换辅助所需类型:



<预类=朗动prettyprint-覆盖> <默认值(DirectCast(没什么,对象) )>




<默认值(CObj(没有))方式> 也不会工作,因为它不是一个恒定的值




幸运的是,不像 CObj(没有),编译器会认为 DirectCast(没什么,对象)&MDASH;令人惊讶和 CTYPE(没什么,对象),太&MDASH;一个恒定值,所以它已被接受


This isn't as trivial as it appears to be. It's a follow-up to this question.

Let's say I have a Windows Forms user control with a property:

// using System.ComponentModel;

[DefaultValue(null)]
public object DataSource { … }

If I translate this to VB.NET, I would try this:

'Imports System.ComponentModel

<DefaultValue(Nothing)>
Public Property DataSource As Object
    …
End Property

This won't work because the compiler has problems choosing the correct overload of DefaultValueAttribute's constructor:

Overload resolution failed because no accessible New is most specific for these arguments:

  • Public Sub New(value As Boolean): Not most specific.
  • Public Sub New(value As Byte): Not most specific.
  • Public Sub New(value As Char): Not most specific.

I am quite certain this is because Nothing in VB.NET not only means "the null reference" (null in C#), but also "the default value for" the parameter type (default(T) in C#). Because of this ambiguity, every constructor overload is a potential match.

<DefaultValue(CObj(Nothing))> also won't work because it is not a constant value.

How exactly do I write this in VB.NET?

P.S.: <DefaultValue(GetType(Object), Nothing)> is an option, but it circumvents the problem. I'm really interested if there's any way to use the same constructor for DefaultValueAttribute as the C# version does.

解决方案

"[…] won't work because the compiler has problems choosing the correct overload of DefaultValueAttribute's constructor[.]"

The compiler can be assisted with a type-cast of Nothing to the desired type:

<DefaultValue(DirectCast(Nothing, Object))>

"<DefaultValue(CObj(Nothing))> also won't work because it is not a constant value."

Fortunately, unlike CObj(Nothing), the compiler considers DirectCast(Nothing, Object) — and suprisingly, CType(Nothing, Object), too — a constant value, so it is accepted.

这篇关于我怎样写[默认值(NULL)在VB.NET? &LT;默认值(没有)GT;不编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆