2014-10-16 140 views
2

这似乎是我应该知道的东西,也许它工作正常,但它看起来不对我。我正在寻找从数据库查询中添加一些数据到一个列表中,我创建了一个自定义的属性类来保存。自定义属性添加到列表

我的类:

Public Class MyClass 
    Private _Col1 As String 
    Private _Col2 As String 
    Private _Col3(,) As Object 
    Private _Col4 As String 

    Public Property Col1() As String 
     Get 
      Return _Col1 
     End Get 
     Set(ByVal value As String) 
      _Col1 = value 
     End Set 
    End Property 

    Public Property Col2() As String 
     Get 
      Return Col2 
     End Get 
     Set(ByVal value As String) 
      Col2= value 
     End Set 
    End Property 

    Public Property Col3() As Object(,) 
     Get 
      Return Col3 
     End Get 
     Set(ByVal value As Object(,)) 
      Col3= value 
     End Set 
    End Property 

    Public Property Col4() As String 
     Get 
      Return Col4 
     End Get 
     Set(ByVal value As String) 
      _Col4= value 
     End Set 
    End Property 

    Sub New(ByVal col1 As String, ByVal col2 As String, ByVal col3 As Object, ByVal col4 As String) 
     _Col1= col1 
     _Col2= col2 
     _Col3 = col3 
     _Col4 = col4 

    End Sub 

End Class 

当我声明并初始化我的列表(类),然后将数据添加到它:

Dim NList as New List(Of MyClass) 

NList.Add(New MyClass("1", "2", (1,3), "3")) 

当我看看列表项,我我在列表中看到8个项目,而不是4个。其中4个是方法,4个是变量。这是否正确以及类属性如何操作?看来我正在通过向4个变量添加数据来浪费资源,并且如果必须循环访问具有40k行的记录集,我认为它会增加性能。这是正确的方式吗?我计划将项目添加到列表中,并在程序的后面使用Find()函数,因此我觉得8个项目而不是4个是不需要的。

编辑:列表项

?NList.Item(0) 
    _Col1: "1" {String} 
    _Col2: "2" 
    _Col3: "(1,3)" 
    _Col4: "4" 
    Col3: "(1,3)" 
    Col4: "4" 
    Col1: "1" 
    Col2: "2" 
+2

当你'看看List项目,你正在查看MyClass的实例而不是列表中的项目,即一行代码将添加一个项目到列表中,您可能会看到私有后台字段以及属性。 (每编辑)很明显的情况:'_Col1'是你的私人支持字段,'Col1'是属性。注意更新的VS版本允许自动实现的道具,这样''公共属性Col1()作为字符串'是你在很多情况下所需要的,这将大大减少课程代码 – Plutonix 2014-10-16 15:43:26

+0

这些都是在上课时被填充的吗?如果我们遍历行并将它们添加到此列表中,这是否会影响性能? – Criel 2014-10-16 15:49:25

+0

只有一件事是被设置的:'_col1',查看'Property Col1()'的代码,它只是设置或返回相同的值 – Plutonix 2014-10-16 15:50:46

回答

1

在你的类:

Public Class MyClass 
    Private _Col1 As String ' private backing field to hold the value 

    ' interface to get/set that same value 
    Public Property Col1() As String 
     Get 
      Return _Col1 
     End Get 
     Set(ByVal value As String) 
      _Col1 = value 
     End Set 
    End Property 

你的类将只获得该属性值一个变量。在你调试,你看到这两个后备字段和属性界面:

?NList.Item(0) 
    _Col1: "1" {String}  <== backing field storing the value 
    ... 
    Col1: "1"     <== property 'exposing' _Col1 

请注意,您的类可以使用自动实现的属性,并放弃明确的支持领域被大大简化:

Public Class fooClass 
    Public Property Col1() As String 
    Public Property Col2() As String 
    Public Property Col3() As DateTime 
    Public Property Col4() As Integer 

    ' VS creates hidden backing fields you can still reference in the class: 
    Public Sub New(str As String ...) 
     _Col1 = str 
     ... 
    End Sub 
End Class 
2

这是完全正常的行为。那些带下划线的变量是类的成员变量。他们实际上包含您的数据。另一方面,你的物业根本就没有任何数据。你可以把它们看作是把你的成员变量返回给调用者的函数。你可以在你的直接提示中看到它们的返回值,因为它们被视为正常变量。

一起使用成员变量和属性是编程.Net时的常用方法。 如果你没有在你的属性执行任何额外的代码,你可以这样写:

Public Property MyProperty As SomeType 

在这种情况下,编译器会自动生成一个无形的相应成员变量为您服务。

1

我修改了一下你的示例代码。也许这会有帮助?

Public Class TestClass 
    Public Property Col1 As String 
    Public Property Col2 As String 
    Public Property Col3 As Object 
    Public Property Col4 As String 
End Class 

项目添加到列表中这样

Dim NList As New List(Of TestClass) 

    NList.Add(New TestClass With {.Col1 = "1", .Col2 = "2", .Col3 = "3", .Col4 = "4"}) 
    NList.Add(New TestClass With {.Col1 = "A", .Col2 = "B", .Col3 = "C", .Col4 = "D"})