问题描述
在查看了有关装箱和拆箱的各种信息源之后,我修改了原始的简单代码.
格式代码:
After reviewing various sources of information about boxing and unboxing, I revised the original simple code.
The form code:
Public Class Form1
Dim TestCL As New C_Test1
Dim jObj As New Object
Private Sub Form1_Load() Handles MyBase.Load
' first test using a simple variable
jObj = CType(100, Object)
TestCL.SetItem(jObj)
MsgBox("First pass Test: Using a simple variable" & vbCrLf & _
"Original value = " & CType(TestCL.GetItem, Short))
jObj = CType(299, Object)
MsgBox("Second pass Test: Using a simple variable" & vbCrLf & _
"Revised value = " & CType(TestCL.GetItem, Short))
End Sub
End Class
课还是像以前一样
The class is as before
Public Class C_Test1
Dim mItem As Object
Public Sub SetItem(ByRef jitem As Object)
mItem = jitem
End Sub
Public Function GetItem() As Object
Return mItem
End Function
End Class
即使强制将变量jObj转换为对象类型(
Even after forcinging the conversion of the variable jObj to an object type (
jObj = CType(100, Object)
),该类仍将其视为整数.我在从Visual Studio运行程序并在逐步执行过程中观察lcoals窗口中的变量类型时观察到了这一点.在第二遍将变量jObj更改为"299"时,该类返回的值不会更改.
所以,然后我尝试了另一种方法.我在表单中添加了2个texbox和一个按钮.这使我可以输入原始值(该值属于该类)和第二个输入.两者都分配给jObj.
), the class still sees this as an integer. I observed this in running the program from Visual Studio and observing the variable type in the lcoals window while stepping through the execution. The value returned by the class does not change when the variable jObj is changed to "299" on the second pass.
So, then I tried another approach. I added 2 texboxes and a button to the form. This allows me to input the original value (that gets assigend to the class) and a second input. Both are assigned to jObj.
Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Dim tmp As Short = CShort(TextBox1.Text)
jObj = CType(tmp, Object)
TestCL.SetItem(jObj)
End Sub
Private Sub TextBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Leave
Dim tmp As Short = CShort(TextBox1.Text)
jObj = CType(tmp, Object)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("class return value = " & TestCL.GetItem)
End Sub
结果与以前相同.当jObj更改为"299"时,该类不会更改mItem的值.实际上,仅单步执行程序,该类就不会将其报告为对象,而仅是对值100的短路的引用.在两种情况下,这种情况基本上都会发生.
我们如何获得一个指向堆栈中项目的指针?在旧的VB6中这是可行的,我知道它在C语言中是可行的.如何在VB.NET中完成?
信息来源之一是:
http://msdn.microsoft.com/en-us/magazine/cc188935.aspx [ ^ ]
The results are the same as before. When jObj is changed to "299", the class does not changed the value of mItem. In fact, single stepping through the program, the class does not report this as an object, but merely a refernce to a short with a value of 100. This basically occurs in both cases tested.
How do we get a pointer to an item on the stack? In old VB6 this was doable and I know it is doable in C. How is it done in VB.NET?
One of the inormation sources was:
http://msdn.microsoft.com/en-us/magazine/cc188935.aspx[^]
推荐答案
这篇关于了解参考变量-第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!