VB.Net'Overridable'在成员变量声明中无效

时间:2013-05-21 16:41:04

标签: c# vb.net variables override

我正在使用几个代码转换器生成相同的VB.Net编码,但除了这行代码之外VS不会:

Private Overridable m_Products As ICollection(Of Product)

VS州:

'Overridable'在成员变量声明中无效。

C#编码来自ASP.Net网站上的教程:

http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer

VS还声明我应该删除Overridable关键字。如果我这样做,我会在教程中打破一些东西吗?

这是我在转换器中运行的C#编码:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace WingtipToys.Models
{
    public class Category
    {
        [ScaffoldColumn(false)]
        public int CategoryID { get; set; }

        [Required, StringLength(100), Display(Name = "Name")]
        public string CategoryName { get; set; }

        [Display(Name = "Product Description")]
        public string Description { get; set; }

        public virtual ICollection<Product> Products { get; set; }
    }
}

这是转换的结果:

Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations

Namespace WingtipToys.Models
Public Class Category
    <ScaffoldColumn(False)> _
    Public Property CategoryID() As Integer
        Get
            Return m_CategoryID
        End Get
        Set
            m_CategoryID = Value
        End Set
    End Property
    Private m_CategoryID As Integer

    <Required, StringLength(100), Display(Name := "Name")> _
    Public Property CategoryName() As String
        Get
            Return m_CategoryName
        End Get
        Set
            m_CategoryName = Value
        End Set
    End Property
    Private m_CategoryName As String

    <Display(Name := "Product Description")> _
    Public Property Description() As String
        Get
            Return m_Description
        End Get
        Set
            m_Description = Value
        End Set
    End Property
    Private m_Description As String

    Public Overridable Property Products() As ICollection(Of Product)
        Get
            Return m_Products
        End Get
        Set
            m_Products = Value
        End Set
    End Property
    Private Overridable m_Products As ICollection(Of Product)
End Class
End Namespace

2 个答案:

答案 0 :(得分:5)

您可以制作属性 Overridable,但不能制作字段

' The property here is fine to be overridable
Public Overridable Property Products() As ICollection(Of Product)
    Get
        Return m_Products
    End Get
    Set
        m_Products = Value
    End Set
End Property

' The backing field cannot be
Private m_Products As ICollection(Of Product)

派生类可以重新实现和覆盖属性,这可能导致后备字段未使用,但它们不能直接覆盖字段。

答案 1 :(得分:3)

字段不能覆盖,但属性可以覆盖。我不确定为什么转换器会创建一个支持字段,您可以使用VB.NET的自动属性(如果您使用的是Visual Studio 2010或更高版本)。试试这个:

Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations

Namespace WingtipToys.Models
Public Class Category
    <ScaffoldColumn(False)> _
    Public Property CategoryID As Integer

    <Required, StringLength(100), Display(Name := "Name")> _
    Public Property CategoryName As String


    <Display(Name := "Product Description")> _
    Public Property Description As String         

    Public Overridable Property Products As ICollection(Of Product)
End Class
End Namespace