在我在网上读到的许多文章中,都提到在vb.net中创建属性时,应该使用get/set方法和类中的一个私有/受保护的成员变量。
就像这样:
Public Class Person
Private _name as string
public property Name as string
get
return _name
end get
set(byval value as string)
_name = value
end set
end property
end class
如果属性的get/集合中没有逻辑,为什么不像这样
在使用匿名类型时,我发现了GroupBy在C#和VB.net中的结果不同。具体来说,当任何键都是可空类型且没有值集时,VB.net似乎无法正确地对项进行分组。
假设我有以下模型和一些数据:
public class Record
{
public int? RecordTypeId { get; set; }
public int? PlayerId { get; set; }
public int? TeamId { get; set; }
public int? Salary { get; set; }
为什么接口重写类定义并违反类封装?我在下面包含了两个样本,一个在C#中,一个在VB.net中。
VB.net
Module Module1
Sub Main()
Dim testInterface As ITest = New TestMe
Console.WriteLine(testInterface.Testable) ''// Prints False
testInterface.Testable = True ''// Access to Private!!!
(这与有关)
如果您定义了一个只有getter属性的接口(= ReadOnly in VB.NET),为什么可以在用C#实现类中定义setter,而不能用VB实现类呢?
我认为它是在.NET级别定义的,而不是特定于语言的。
示例:用于此接口
'VB.NET
Interface SomeInterface
'the interface only say that implementers must provide a value for reading
ReadOnly Property PublicProperty As String
End Interfa
我喜欢.NET自动属性,在C#中,通过将其set部分声明为private非常容易,如下所示:
public String Name{ get; private set; }
但是当我尝试在VB.NET中使用它时,我感到震惊的是,它不像前面提到的那样被支持,我不得不这样写它:
Private _Name as String
Public ReadOnly Property Name as String
Get
return _Name
End Get
End Property
或者:
Private _Name as String
Public Property Name
在VB.NET中,您可以更改此变量的名称。您也可以在C#中这样做吗?还是它总是称为value?
示例:
Private _SomeVariable As String
Public Property SomeValue() As String
Get
Return _SomeVariable
End Get
Set(ByVal foo As String) ' This is what I mean
_SomeVariable = foo
End Set
将一个旧的ASP站点迁移到.NET,并且有大量的VBScript我们正在迁移到VB.NET。
遇到了旧的property语法的麻烦,有没有人知道有什么工具(或其他类似的魔法)可以自动完成这项工作?
我查过了,但没有找到joy。
谢谢邓肯
编辑: VB脚本属性如下所示:
Public Property Get AgeCategoryID()
AgeCategoryID = m_AgeCategoryID
End Property
Public Property Let AgeCategoryID(Value)
m_AgeCategoryID=Value
End Property
在Vb.net中,这段小代码“调用”或“执行”是什么?
具体地说就是<>_之间的东西。我理解这个属性在做什么。我只是不确定上面这条线的意义。
<TheApp.DataHandler.ColumnAttributes("BillingClientName")> _
Public Property BillingClientName As String
Get
Return _BillingClientName
End Get
Set(ByVal value As String)
_BillingCli
假设我有两个接口,如下所示:
public interface ISkuItem
{
public string SKU { get; set; }
}
public interface ICartItem : ISkuItem
{
public int Quantity { get; set; }
public bool IsDiscountable { get; set; }
}
当我在C#中实现接口时,VS生成以下模板代码:
public class CartItem : ICartItem
{
#region ICartItem Members
我有这两个实体
public class Song : IPathHavingEntity
{
public int Id { get; set; }
[Required]
public string Path { get; set; }
[Required]
public virtual Album Album { get; set; }
[Required]
public int TrackNumber { get; set; }
}
public class
大家好,我是c#语言新手,我使用的是vb.net,下面这段代码的错误是什么,为什么,谢谢
vb.net code
Class SplitString
Implements IEnumerable
Implements IEnumerator
Private currentPosition As Integer = 0
Private m_Sentence As String
Property Sentence() As String
Get
Return m_Sentence
End Get
Set(ByVal Value As String)
当以属性作为参数调用函数时,属性的set方法将在函数调用后执行。
如果您试图将属性传递给使用c#的函数,这会抛出一个编译器错误(如果在ref中执行),但是在vb.net中会这样做。这是个虫子吗?到底怎么回事?
Module Module1
Private _testProp As Integer
Property testProp As Integer
Get
Return _testProp
End Get
Set(value As Integer)
Console.WriteL
任何人都有任何想法为什么这不工作(C#或VB.NET或其他.NET语言无关紧要)。这是我的问题的一个非常简单的例子(对不起VB.NET):
Private itsCustomTextFormatter As String
Public Property CustomTextFormatter As String
Get
If itsCustomTextFormatter Is Nothing Then CustomTextFormatter = Nothing 'thinking this should go into the
所以如果我有:
public class ChildClass : BaseClass
{
public new virtual string TempProperty { get; set; }
}
public class BaseClass
{
public virtual string TempProperty { get; set; }
}
如何使用反射来查看ChildClass是否隐藏了TempProperty的基本实现?
我希望答案在c#和vb.net之间是不可知的
如何更改Entity Framework6下的VB.NET T4模板,使其改为生成属性
Public Property name As String
它会生成
Public Property name As String
Get
Return _name
End Get
Set(ByVal value As String)
[Set](_name , value, "name")
End Set
End Property
Private _name As String
有没有办法像在C#中那样,在VB.NET中将公共属性放在一行中?每次我尝试将所有内容移动到一行时,都会收到一堆错误。
C#:
public void Stub{ get { return _stub;} set { _stub = value; } }
VB.NET
Public Property Stub() As String
Get
Return _stub
End Get
Set(ByVal value As String)
_stub = value
End Set
End Property
谢谢
编辑:我应该澄清一下,
我在VB.NET里发现了一些奇怪的东西,从来没有注意到.
我在 VB.NET 中有一个具有参数的属性的类,我希望在其他C#类中使用该属性,方法是创建VB.NET类的对象,但类对象没有显示该属性,有人能告诉我是否可以将该属性访问到C#中。
如果是,怎么做?如果不是,CLR是什么意思?
这是我的密码。
Public Property AsString(ByVal name As String) As String
Get
//Some code
End Get
Set(ByVal value As String)
//Some
在C#中,您不能具有索引属性。也就是说,如何将以下代码从VB.net转换为C#?
Private _PatchSpectrum(49) As Double
Public ReadOnly Property GetPatchSpectrum() As Double()
Get
Return _PatchSpectrum
End Get
End Property
Public WriteOnly Default Property PatchSpectrum(idx As Integer) As Double
Set(ByVal value as Dou
我正在学习vb.net,但在理解以下内容时遇到了困难……
我可以创建自定义类的多个属性并获取/设置值,也可以创建一个方法一次设置所有这些属性。如果每个属性都允许读写,我应该只创建一个一次性赋值的方法吗?我假设我在这里遗漏了一个非常重要的部分。示例:
我可以创建两个属性:
Public Class Employee
Public Property LastName as string
Get
Return strLastName
End get
Set(ByVal value as string)
我有一个MedicineData类
Public Class MedicineDataDB
Public Sub New(ByVal dtr As DataRow)
Id = Int32.Parse(dtr("Id"))
MedicineName = dtr("medicineName").ToString()
BookName = dtr("book").ToString()
AuthorName = dtr("author").ToString()
我设置了一个类来保存注册表单(VB.NET,MVC)上的值,在这些属性中有一个Password属性:
Public Class RegisterModel
...
Private _password As String
<DisplayName("Password:"), Required(), ValidatePasswordLength(), DataType(DataType.Password)> _
Public Property Password() As String
Get
R
我在c#类库中有以下代码..。
public static class foo
{
public enum bar
{
bsNone = -1,
bsSolid = 0,
bsDash = 1
}
}
在VB.Net Winforms应用程序中,我将枚举引用为属性的返回类型:
Private _LeftBorderStyle As foo.bar
Public Property LeftBorderStyle() As foo.bar
Get
Return _LeftBorderStyle
我已经将一个vb6控件迁移到了vb.net,它引用了FARPoint扩展,下面是VB6代码。
Public Property Let RetainSelBlock(ByVal New_RetainSelBlock As Boolean)
sprSpread.RetainSelBlock() = New_RetainSelBlock
PropertyChanged "RetainSelBlock"
End Property
下面的代码是向导从上面的VB.Net代码生成的Vb6代码。
Public Property RetainSelBlock() A
在VB.Net属性中,可以像在函数中一样接收参数,方法是在属性定义的开头提及所有参数:
Private m_Address(2) As String
Property Address(ByVal index As Integer) As String
Get
If index >= 0 And index <= UBound(m_Address) Then
Return m_Address(index)
End If
End Get
Set(ByVal Value As String)
If index >= 0
我注意到了Visual 2008 (.NET 3.5)和VisualStudio2010Beta2 (.NET 4.0)中的一些看似奇怪的问题。这些问题可能在以前的版本中也存在。也许它们不是一个问题,但不管怎样,在我提交关于Microsoft的报告之前,我想看看是否有合理的解释。
设置(在VB中,C#的结果不同,后面将包含在post中):
Public Class SomeClass
Public Property SomeProperty() As String
Get
Return String.Empty
End Get
我正在.NET中创建一个包装类(碰巧是VB.NET,但与C#同样相关),它暴露在COM中,而我试图包装的属性之一是变体。我以为我可以使用一个对象,但是我得到了一个错误:
Public Property FieldValue([vFieldID As Object = -1]) As Object不能作为属性“让”公开给COM。您将无法使用'Let‘语句将VisualBasic6.0中的非对象值(如数字或字符串)分配给此属性。
我的财产声明如下:
Public Property FieldValue(Optional ByVal vFieldID As Object = -1)
有人能帮上忙吗。
我在vb.net中有以下内容,需要将其转换为c#。起初看起来相当简单,但我还需要传入NamedObject变量l,该变量在vb.net中受支持,但在c#中不受支持。
我的选择是什么。
这是vb.net -请注意NamedObject
Public Property Datos(ByVal NamedObject As String) As T
Get
Return CType(HttpContext.Current.Session.Item(NamedObject ), T)
End Get
Se
我有一个用VB.net编写的基类。这个基类有一个属性。我有一个从那个vb类驱动的C#类。我需要在这里设置属性,这样我才能给它赋值。这是我在vb.net类中的属性:
Public MustInherit Class FTPDownloaderBase
Public WriteOnly Property FilesToDownload() As ArrayList
Set(ByVal Value As ArrayList)
_filesToDownload = Value
End Set
End Property
这是我的C#类:
public c
我经常在vb.net中重复这样做。
Private _nerfed As Boolean
Public ReadOnly Property nerfed As Boolean
Get
Return _nerfed
End Get
End Property
我想知道有没有短手的?
我知道
Private _nerfed As Boolean
Public Property nerfed As Boolean
Get
Return _nerfed
End Get
Set (nerfed)
_nerfed =ne
如何将以下代码转换为VB.NET?
class A
{
public int NumberA { get; set; }
}
class B : A, I
{
public int NumberB { get; set; }
}
interface I
{
int NumberA { get; set; }
int NumberB { get; set; }
}
在VB.NET中,属性声明后的Implements关键字存在问题。所以我需要这样做:
Class B
Inherits A
Implements IC
Public Pro
好吧,我已经盯着屏幕看了几个小时了,我不知道为什么我会收到这个错误。我已经在许多其他项目中使用了Code First,并且以前没有遇到过任何问题……
下面是错误:
System.InvalidOperationException was unhandled by user code
Message=The properties expression 'sci => sci.ShoppingCartItemId' is not valid. The expression should represent a property: C#: 't => t.My
我找到以下语法作为VB.NET属性,并尝试将其转换为c#,但我不确定如何实现这一点。
Public Property SomeText(ByVal someEnumThing as SomeEnum) As String
Get
Select Case someEnumThing
//figure out what string to return
End Select
End Get
Set(ByVal Value as String)
Select Case someEnumThing
我有一个JSON字符串,我试图在VB.NET对象中反序列化它,但是我只得到了任何东西/空值。下面是我正在做的VB.net代码。对象,以及我发送给反序列化的内容的字符串版本,以及我要返回的内容。我应该把我路过的东西拿回来。以下是Chrome控制台的一些信息,可能会有所帮助。。红色是我路过的,黄色是我要回来的东西。
谢谢!
VB.NET
Dim serializer As New JavaScriptSerializer()
Dim response As IMSClassLibrary.PhotoModelManagerCL.objOutfitModel = serializer
这在带有属性的VB.NET中似乎是不可能的,因为属性语句本身必须描述它是否是ReadOnly。
在下面的示例中,它没有让我编译ReadWriteChild。我想我可以让父进程读/写,然后让ReadOnlyChild的setter什么都不做,但这看起来有点老土。在这种情况下,最好的选择似乎是放弃属性,转而使用getter/setter方法。
Public MustInherit Class Parent
Public MustOverride ReadOnly Property Foo() As String
End Class
Public Class ReadOnlyChild
我对在VB.net中使用GET & SET方法的必要性感到有点困惑。我想在这方面讨论两种情况:首先,当我们将属性声明为PUBLIC时;其次,当我们将属性声明为PRIVATE时。 我发现,当我将一个属性定义为public时,我可以直接设置和访问该属性的值,而无需使用GET/SET方法-非常简单:见下文 Module Program
Sub Main()
Dim t As New test()
t.name = "Roy" 'Label 1
Console.WriteLine("t.name = {0
是否可以像在VB.NET中一样在Objective-C中创建自定义属性属性?例如,在VB.NET中,您可以创建"Browsable“属性并在运行时读取它,以确定是否应该显示属性。
Public Class Employee
<Browsable(True)> _
Public Property Property1() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property
<Brows
我有一个非常简单的类,它位于我的VS2008 web应用程序项目的App_Code文件夹中。我正在尝试从我的代码隐藏文件实例化这个类的一个实例。Intellisense似乎看不到我的类,我不确定为什么。我使用的是与C#相比我不太熟悉的VB.NET。也许我错过了什么。我敢打赌,这与我在VB.NET中遗漏的一些东西有关。
下面是我的简单类(用于测试):
Public Class mySimpleClass
'Private member variables whose data is obtained from user input
Private mUserID as S
如果模型在C#中声明了字符串,则如下所示:
public string Message { get; set; }
我使用C# VB.net ,我的字符串现在变成:
Public Property Message() As String
Get
Return m_Message
End Get
Set
m_Message = Value
End Set
End Property
Private m_Message As String
在声明字符串时,我想避免太多的代码行。如果我在一个视图模型中有50个字符串,它就会变得笨重。
这在
我刚刚开始使用抽象类,但我对抽象类中抽象属性的使用感到有点好奇。为什么你要在一个已经抽象的类中创建一个抽象的属性?无论其是否抽象,该属性都将被公开。在什么情况下,在抽象类中抽象属性是一种优势?也许有一点愚蠢/基本的问题,但到目前为止,我还没有遇到过这样的情况:我对自己说:“这个确切的属性应该在这个抽象类中是抽象的”。
考虑这个例子..你会把任何属性抽象起来吗?为什么?
public abstract class BaseError
{
public string Title { get; set; } = "";
public string Descriptio