在.net中,如果一个函数返回一个对象,那么使用该函数是不对的,_as_一个对象?

时间:2022-09-06 12:31:04

I've got a f(x) that returns a collection of user controls. .Net lets me just treat the f(x) name as the collection. Is there something wrong with doing so?

我有一个f(x)返回一组用户控件。 .Net让我只将f(x)名称视为集合。这样做有什么问题吗?

ex)

Private Function GetCcB() As Collection(Of Reports_ucColumn)
    Dim cc As New Collection(Of Reports_ucColumn)
    cc.Add(Me.ucColumn0)
    cc.Add(Me.ucColumn1)
    cc.Add(Me.ucColumn2)
    Return cc
End Function

Then later on, all use the properties on the objects in the collection, like this:

然后,所有人都使用集合中对象的属性,如下所示:

    For i As Integer = 0 To csB.Count - 1
        If csB(i).Contains("Invalid") Or csB(i).Contains("Duplicate") Then
            GetCcB.Item(i).isValid = False
            GetCcB.Item(i).title = csB(i)
            If csB(i).Contains("Invalid") Then : GetCcB.Item(i).errCode = "005W"
            Else : GetCcB.Item(i).errCode = "005W" 'todo - chg this once we get a duplicate col err code
            End If
        Else
            GetCcB.Item(i).isValid = True
            GetCcB.Item(i).title = csB(i)
            GetCcB.Item(i).errCode = String.Empty
        End If

3 个解决方案

#1


By calling the method each time that you use the collection, what your code is effectively doing is:

通过每次使用集合时调用该方法,您的代码实际执行的操作是:

For i As Integer = 0 To csB.Count - 1
   Dim cc As Collection(Of Reports_ucColumn)
   If csB(i).Contains("Invalid") Or csB(i).Contains("Duplicate") Then
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).isValid = False
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).title = csB(i)
      If csB(i).Contains("Invalid") Then
         cc = New Collection(Of Reports_ucColumn)
         cc.Add(Me.ucColumn0)
         cc.Add(Me.ucColumn1)
         cc.Add(Me.ucColumn2)
         cc.Item(i).errCode = "005W"
      Else
         cc = New Collection(Of Reports_ucColumn)
         cc.Add(Me.ucColumn0)
         cc.Add(Me.ucColumn1)
         cc.Add(Me.ucColumn2)
         cc.Item(i).errCode = "005W" 'todo - chg this once we get a duplicate col err code
      End If
   Else
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).isValid = True
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).title = csB(i)
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).errCode = String.Empty
   End If
Next

You would never write code like that, would you?

你永远不会写那样的代码,对吗?

You can turn the function into a property with lazy initialisation. Then it's ok to use it over and over, as the collection is only created once:

您可以将函数转换为具有延迟初始化的属性。然后可以一遍又一遍地使用它,因为集合只创建一次:

Private _cc As Collection(Of Reports_ucColumn)

Private Property CcB As Collection(Of Reports_ucColumn)
   Get
      If _cc Is Nothing Then
         _cc = New Collection(Of Reports_ucColumn)
         _cc.Add(Me.ucColumn0)
         _cc.Add(Me.ucColumn1)
         _cc.Add(Me.ucColumn2)
      End If
      Return _cc
   End Get
End Property

(Ideally the property should be public in a class and the _cc variable should be private, encapsulated in the class so that only the property could access it.)

(理想情况下,属性应该在类中是公共的,_cc变量应该是私有的,封装在类中,以便只有属性才能访问它。)

Semantically this works fine also. A function generally does a bit of work, so you shouldn't call it over and over if you want to use the result over and over. A property on the other hand usually doesn't do much work, and if the result needs some processing to be created, the property usually does something like lazy initialisation or pre-initialisation to make sure that the work isn't done more than neccessary.

从语义上讲,这也可以。函数通常会做一些工作,因此如果要反复使用结果,则不应反复调用它。另一方面,属性通常不会做太多工作,如果结果需要创建一些处理,则属性通常会执行惰性初始化或预初始化等操作,以确保工作不会超过必要的工作。 。

#2


Well, you keep creating new collections in memory each time you call GetCcB, so I would recommend using a variable instead. =)

好吧,每次调用GetCcB时,你都会在内存中创建新的集合,所以我建议改用变量。 =)

Edit: In essence, the object (collection) that GetCcB returns is different (though not in content) each time you call it and takes up additional memory on the heap which has to be collected by the garbage collector when it makes a pass. Better to just grab the collection as a variable and use that.

编辑:实质上,每次调用它时,GetCcB返回的对象(集合)都是不同的(虽然不是内容),并占用堆上的额外内存,垃圾收集器在通过时必须收集这些内存。最好只将集合作为变量抓取并使用它。

#3


You'd want to use a singleton instead. It's a static class with a property that returns a private field of the class.

你想要使用单身人士。它是一个静态类,其属性返回类的私有字段。

#1


By calling the method each time that you use the collection, what your code is effectively doing is:

通过每次使用集合时调用该方法,您的代码实际执行的操作是:

For i As Integer = 0 To csB.Count - 1
   Dim cc As Collection(Of Reports_ucColumn)
   If csB(i).Contains("Invalid") Or csB(i).Contains("Duplicate") Then
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).isValid = False
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).title = csB(i)
      If csB(i).Contains("Invalid") Then
         cc = New Collection(Of Reports_ucColumn)
         cc.Add(Me.ucColumn0)
         cc.Add(Me.ucColumn1)
         cc.Add(Me.ucColumn2)
         cc.Item(i).errCode = "005W"
      Else
         cc = New Collection(Of Reports_ucColumn)
         cc.Add(Me.ucColumn0)
         cc.Add(Me.ucColumn1)
         cc.Add(Me.ucColumn2)
         cc.Item(i).errCode = "005W" 'todo - chg this once we get a duplicate col err code
      End If
   Else
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).isValid = True
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).title = csB(i)
      cc = New Collection(Of Reports_ucColumn)
      cc.Add(Me.ucColumn0)
      cc.Add(Me.ucColumn1)
      cc.Add(Me.ucColumn2)
      cc.Item(i).errCode = String.Empty
   End If
Next

You would never write code like that, would you?

你永远不会写那样的代码,对吗?

You can turn the function into a property with lazy initialisation. Then it's ok to use it over and over, as the collection is only created once:

您可以将函数转换为具有延迟初始化的属性。然后可以一遍又一遍地使用它,因为集合只创建一次:

Private _cc As Collection(Of Reports_ucColumn)

Private Property CcB As Collection(Of Reports_ucColumn)
   Get
      If _cc Is Nothing Then
         _cc = New Collection(Of Reports_ucColumn)
         _cc.Add(Me.ucColumn0)
         _cc.Add(Me.ucColumn1)
         _cc.Add(Me.ucColumn2)
      End If
      Return _cc
   End Get
End Property

(Ideally the property should be public in a class and the _cc variable should be private, encapsulated in the class so that only the property could access it.)

(理想情况下,属性应该在类中是公共的,_cc变量应该是私有的,封装在类中,以便只有属性才能访问它。)

Semantically this works fine also. A function generally does a bit of work, so you shouldn't call it over and over if you want to use the result over and over. A property on the other hand usually doesn't do much work, and if the result needs some processing to be created, the property usually does something like lazy initialisation or pre-initialisation to make sure that the work isn't done more than neccessary.

从语义上讲,这也可以。函数通常会做一些工作,因此如果要反复使用结果,则不应反复调用它。另一方面,属性通常不会做太多工作,如果结果需要创建一些处理,则属性通常会执行惰性初始化或预初始化等操作,以确保工作不会超过必要的工作。 。

#2


Well, you keep creating new collections in memory each time you call GetCcB, so I would recommend using a variable instead. =)

好吧,每次调用GetCcB时,你都会在内存中创建新的集合,所以我建议改用变量。 =)

Edit: In essence, the object (collection) that GetCcB returns is different (though not in content) each time you call it and takes up additional memory on the heap which has to be collected by the garbage collector when it makes a pass. Better to just grab the collection as a variable and use that.

编辑:实质上,每次调用它时,GetCcB返回的对象(集合)都是不同的(虽然不是内容),并占用堆上的额外内存,垃圾收集器在通过时必须收集这些内存。最好只将集合作为变量抓取并使用它。

#3


You'd want to use a singleton instead. It's a static class with a property that returns a private field of the class.

你想要使用单身人士。它是一个静态类,其属性返回类的私有字段。