了解参考变量-第2部分 [英] Understanding Reference Variables - Part 2

查看:118
本文介绍了了解参考变量-第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看了有关装箱和拆箱的各种信息源之后,我修改了原始的简单代码.

格式代码:

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[^]

推荐答案

请参阅我对问题的评论和对上一个问题的评论.不幸的是,您仍然不了解这些内容,并且您的代码示例看起来毫无意义,至少没有我在那些注释中提到的正确解释.

只有一个提示可以解释所有进一步的混乱:两次分配jObj,并且每次通过构造函数创建一个全新的对象时,都不要引用相同的对象.这样,对该对象的引用将在此变量中被覆盖.第一个仍然存在,但它变得不可达.因此,最终,垃圾收集器(GC)销毁了它.请参阅:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ].

请注意对象的可达性的作用:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Reachability_of_an_object [ ^ ].

—SA
Please see my comment to the question and my comments to your previous question. Unfortunately, you still did not understand things, and your code samples look pretty much pointless, at least without proper explanations I mentioned in those comments.

Just one hint which should explain all of your further confusions: you assign jObj twice, and each time you create a brand-new object through a constructor, not reference the same object. This way, the reference to the object becomes overwritten in this variable. The first still exists, but it becomes unreachable. So, eventually, Garbage Collector (GC) destroys it. Please see:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

Please pay attention for the role of reachability of an object:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Reachability_of_an_object[^].

—SA


Kevin,

[edit]
请参阅以下文章:
http://stackoverflow. com/questions/1198281/how-do-i-get-an-intptr-pointing-to-an-integer-in-vb-net [探索元帅类的常规功能 [ ^ ]
[/edit]

希望对您有所帮助;)
Kevin,

[edit]
See these articles:
http://stackoverflow.com/questions/1198281/how-do-i-get-an-intptr-pointing-to-an-integer-in-vb-net[^]
Exploring general functions of the Marshal class[^]
[/edit]

I hope it will be helpful ;)


这篇关于了解参考变量-第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆