Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write [DefaultValue(null)] in VB.NET? <DefaultValue(Nothing)> does not compile

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.

like image 321
stakx - no longer contributing Avatar asked Apr 20 '15 13:04

stakx - no longer contributing


Video Answer


1 Answers

"[…] 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.

like image 188
stakx - no longer contributing Avatar answered Oct 16 '22 10:10

stakx - no longer contributing