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.