【问题标题】:Overload resolution failed because no accessible 'New' is most specific for these arguments:重载解析失败,因为没有可访问的“新”对这些参数最具体:
【发布时间】:2014-04-24 10:22:40
【问题描述】:

我有一个带有中继器的用户控件。最初在页面加载中,我有从数据库中获取数据并绑定到转发器的代码。我现在想在用户控件之外使用这个功能,这样我就可以在页面上拥有多个并让它们绑定到不同的数据。

我现在的代码是:

Imports System.ComponentModel

Public Class UpdateList
    Inherits System.Web.UI.UserControl

    Private m_dataSource As Object

    <TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")> _
    <Category("Data")> _
    <DefaultValue(Nothing)> _
    Public Property DataSource() As Object
        Get
            Return Me.m_dataSource
        End Get
        Set(value As Object)
            If Me.m_dataSource <> value Then
                m_dataSource = value
                tryDataBinding()
            End If
        End Set
    End Property

    Public ReadOnly Property UpdateCount As Integer
        Get
            Return m_UpdateCount
        End Get
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub

    Protected Sub tryDataBinding()

        rep_Updates.DataSource = Me.m_dataSource
        rep_Updates.DataBind()

    End Sub

End Class

我在&lt;DefaultValue(Nothing)&gt; 收到一条波浪线并收到错误:

重载解析失败,因为没有可访问的“新”最适合这些参数:

'Public Sub New(value As Boolean)':不是最具体的。

'Public Sub New(value As Byte)':不是最具体的。

'Public Sub New(value As Char)':不是最具体的。

这是什么意思?谢谢

更新

解决方法是将数据源的属性声明更改为...

    Private m_dataSource As Object

    Public Property DataSource() As Object
        Get
            Return Me.m_dataSource
        End Get
        Set(value As Object)
            m_dataSource = value
            tryDataBinding()
        End Set
    End Property

【问题讨论】:

  • 至少在 WinForms 中 DefaultValue 属性 ctor 不能为 Nothing。由于它没有定义初始起始值,而是定义何时保留属性值的比较值 - 在这种情况下无论如何看起来都很微妙,只需删除该属性即可。
  • @Plutonix 谢谢...我将 sn-p 作为 c# - 只是将其转换并粘贴到我的代码中,没有发现 System.Windows.Forms... 删除 TypeConverter、Category 和 DefaultValue 就可以了.发布为答案,我会标记。

标签: vb.net data-binding user-controls


【解决方案1】:

至少在 WinForms 中 DefaultValue 属性 ctor 不能是 Nothing(在对象浏览器中没有这样的定义)。 DefaultAttribute 没有定义初始起始值(尽管有名称),而是定义何时保留属性值的比较值。在 webform 和数据源的情况下,这似乎是可疑的,所以只需删除该属性。

正如您所说,TypeConverter 也可能不合适。

【讨论】:

  • 我相信这实际上是错误的。 DefaultValueAttribute 至少有一个 fitting constructor 可以传递空引用;问题是 VB.NET 的 Nothing 不仅仅意味着“空引用”,它还意味着参数类型的“默认值”,因此匹配每一个构造函数重载;编译器不知道该选择哪一个。 See my answer.
  • 不,事实上并非不正确。 BooleanByteChar 等是类型。 Nothing 不是类型,它没有重载。您说得对,部分问题在于 VB Nothing null。您使用 DirectCast 的新答案既聪明又值得,但实际上它 converting Nothing 到 Type 所以它适合其中一个角色。
  • 当然Nothing 不是类型。那不是重点。您的回答实际上是错误的,因为您声称 DefaultAttribute ctor 不能什么都不是(对象浏览器中没有这样的定义)”。除非我误解了您的(有些不精确)的措辞,否则这显然是错误的:is 有一个接受 object 类型值的 ctor 重载;比如空引用;又名Nothing。如果您在 C# 中编写相同的代码,您会发现您的陈述不可能正确:[DefaultValue(null)] 是完全有效的 C# 代码。
  • (顺便说一句:DefaultValueAttribute 类只有一个版本,这并不特定于 Windows 窗体。因此,如果您对 DefaultValueAttribute 具有或不具有的构造函数进行一些陈述'没有,那么该语句对 Windows 窗体有效,但也适用于使用此自定义属性类的任何其他代码。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多