- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的示例中,我是否在 ByRef 或 ByVal 函数中传递 List(T) 对象有关系吗?
这是对的吗,因为List是引用类型,所以即使我传递对象ByVal,值也总是会改变。
如果我在更新列表时在函数“ListChanged”中传递对象 byRef 会更好吗?
Public Class MyClass_
Public Sub TestMethod()
Dim List_1 As New List(Of Integer)()
Dim List_2 As New List(Of Integer)()
List_1.Add(100)
List_2.Add(50)
List_1 = ActualListNotChanged(List_1) '---101
List_2 = ListChanged(List_2) '---50,51
End Sub
Private Function ActualListNotChanged(ByVal lst As List(Of Integer)) As List(Of Integer)
Dim nList As New List(Of Integer)()
For Each item As Integer In lst
If item <> 50 Then
nList.Add(101)
End If
Next item
Return nList
End Function
Private Function ListChanged(ByVal lst As List(Of Integer)) As List(Of Integer)
lst.Add(51)
Return lst
End Function
End Class
最佳答案
在您的示例中,ByVal(默认值)是最合适的。
ByVal 和 ByRef 都允许您修改列表(例如添加/删除项目)。ByRef 还允许您用不同的列表替换该列表,例如
Dim List1 As New List(Of Int)
List1.Add(1)
ListReplacedByVal(List1)
' List was not replaced. So the list still contains one item
Debug.Assert(List1.Count = 1) ' Assertion will succeed
ListReplacedByRef(List1)
' List was replaced by an empty list.
Debug.Assert(List1.Count = 0) ' Assertion will succeed
Private Sub ListReplacedByVal(ByVal lst As List(Of Integer))
lst = New List(Of Int)
End Sub
Private Sub ListReplacedByRef(ByRef lst As List(Of Integer))
lst = New List(Of Int)
End Sub
一般来说,您应该使用 ByVal。您传递的对象可以被修改(从某种意义上说,您可以调用其方法和属性 setter 来更改其状态)。但它不能被其他对象替换。
关于vb.net - List(T) 的对象是通过 ByVal 还是 ByRef 传递有关系吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19953614/
我有以下代码: Sub AddValidation() Dim ws, wsDefinitions As Worksheet Set ws = ThisWorkbook.Worksheet
我想我最近(终于)开始掌握 ByVal 和 ByRef 在 VBA 中的工作方式。 观察结果 在我迄今为止读过的所有教程和引用文献中,ByRef 始终是“传递对象”,ByVal 是“传递对象的副本”
我正在从 Excel 电子表格中导出多个 INI 文件,其中一行表示一个 INI 文件,INI 部分和键是列。到目前为止,这工作得很好,除了似乎 VBA 正在传递字典 ByRef (?),但让我解释一
我在 Access 2013 中使用 VBA。 在常规模块中有 2 个过程,RunProc() 和 PopulateCollection() 当执行RunProc时,它会调用PopulateColle
我认为我遗漏了一些关于数据表如何工作的基本知识。以下过程以 GetData 开头,引入what作为填充的数据表。以下代码所做的全部工作是传递数据表的副本,对其进行操作,然后返回它: Sub GetDa
我认为我遗漏了有关 DataTables 工作原理的一些基本知识。以下过程以 GetData 开始,将 what 作为填充的 DataTable 引入。以下所有代码所做的就是传递数据表的副本,对其进行
我正在观看一些关于 Nodejs 的介绍类型视频。我遇到了一个我想出来的问题,但我想了解“为什么”。这是有问题的原始代码: var http = require("http"); va
我正在 VBA 上制作一个宏,该宏附加到 excel 中的数据验证列表。当从列表中选择某个选项时,我希望它运行分配给该选择的宏。但是我反复收到错误“编译错误。参数不是可选的。我知道我需要在调用宏后添加
我不需要这个声明,因为它只会使我的代码变得庞大且不可读。 有没有办法让 Visual Studio (VS) 不自动添加它。每次我删除它时,它都会被 VS 添加回来。 Function DoStuff
有什么不同?我总是使用 ByVal,但是,我真的不知道什么时候应该什么时候不应该...... 最佳答案 ByRef = 你给你的 friend 你的学期论文(原件),他会做标记并可以退还给你。ByVa
在 ByRef 和 ByVal 之间进行选择时需要考虑哪些事项。 我理解两者之间的区别,但我不完全理解 ByRef 是否节省资源,或者我们是否需要在 .Net 环境中担心这一点。 如果功能在某种情况下
ByRef 与 ByVal 会生成错误!? 我有一个使用对象的方法 Function Foo(ByRef bar as CustomObject) as Boolean 此方法生成错误,因为一些奇怪的
我尝试尝试 JaredPar ByRef vs ByVal Clarification 回答的问题 ByVal in VB.NET means that a copy of the provided
当我进行提取方法重构时,ReSharper 总是想把 ByVal 修饰符放在任何地方。不再需要显式放置 ByVal 并且 Visual Studio 本身 doesn't do that anymor
我正在从 asp.net 应用程序调用一个 dll(用 vb6 编写)。我正在调用 dll 的以下函数: Public Function FamIdentify_BYTE(strName As Str
我正在学习 C++ 在线类(class) (Pluralsight.com),并在模板上进行练习,特别是将实际数字(例如 3 )传递给需要 (T& t) 的模板。 . 讲师编写的代码无法在我的 PC
我正在使用第三方 C++ dll,它使用具有以下签名的函数: extern "C" __declspec(dllimport) int __stdcall CalcDDtable(struct ddT
我知道 C# 中的默认值是 ByVal。我在很多地方使用了相同的变量名,然后我注意到传递的值发生变化并返回。我想我知道 C# 的作用域机制是错误的。这里公共(public) license 覆盖本地
在 VB.NET 中,用于方法参数更快,ByVal或 ByRef ? 此外,哪个在运行时(RAM)消耗更多资源? 我通读了this question ,但答案不适用或不够具体。 最佳答案 Byval
我正在使用事件处理程序,如果选择一个或两个单元格,我会运行某些事件。我遇到的问题是,当选择两个单元格时,我不知道如何访问第二个单元格的属性(即它的值是什么)。知道如何访问所选的第二个单元格的值(我希望
我是一名优秀的程序员,十分优秀!