我怎样写[默认值(NULL)在VB.NET? <默认值(没有)GT;不编译

编程入门 行业动态 更新时间:2024-10-25 08:27:37
本文介绍了我怎样写[默认值(NULL)在VB.NET? <默认值(没有)GT;不编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

使用System.ComponentModel //; [默认值(NULL)公共DataSource对象{...}

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

'进口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 构造函数[。]

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

<默认值(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;不编译

本文发布于:2023-11-17 03:19:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:默认值   怎样写   NULL   VB   GT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!