gpt4 book ai didi

c# - VB.Net 'Overridable' 对成员变量声明无效

转载 作者:行者123 更新时间:2023-11-30 19:42:52 25 4
gpt4 key购买 nike

我正在使用多个生成相同 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

最佳答案

您可以创建一个属性可覆盖,但不能创建一个字段:

' 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)

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

关于c# - VB.Net 'Overridable' 对成员变量声明无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16675190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com