了解参考变量 [英] Understanding Reference Variables

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

问题描述

我在理解VB.Net中的参考变量时遇到了麻烦.我编写了一组非常简单的代码来演示什么是happing.

首先是一个简单的表格

I am having trouble with my understanding of reference variables in VB.Net. I wrote a very simple set of code to demonstrate what is happing.

First a simple form

Public Class Form1


    Private Sub Form1_Load() Handles MyBase.Load

        Dim v1 As Object
        Dim TestCL As New C_Test1

        '' first test using a simple variable
        v1 = 100

        TestCL.SetItem(v1)

        MsgBox("First Test: Using a simple variable" & vbCrLf & _
               "Original value = " & TestCL.GetItem)

        v1 = 299

        MsgBox("First Test: Using a simple variable" & vbCrLf & _
               "Revised value = " & TestCL.GetItem)

        '' second test using an array
        Dim t(0) As Object

        t(0) = 100

        TestCL.SetItem(t)

        MsgBox("Second Test: Using an array" & vbCrLf & _
               "Original value = " & TestCL.GetItem(0))

        t(0) = 299

        MsgBox("Second Test: Using an array" & vbCrLf & _
                "Revised value = " & TestCL.GetItem(0))


        '' third test using a list
        Dim L As New List(Of Short)
        L.Add(100)

        TestCL.SetItem(L)

        MsgBox("Third Test: Using an list" & vbCrLf & _
               "Original value = " & TestCL.GetItem(0))

        L(0) = 299

        MsgBox("Third Test: Using an list" & vbCrLf & _
               "Revised value = " & TestCL.GetItem(0))


    End Sub


End Class



和班级



And the class

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



v1更改为"299"后,使用简单变量的第一个测试将返回分配的原始值("100").这是问题所在.

将变量更改为"299"后,第二次和第三次测试会产生预期的结果.

我不明白为什么第一次测试不起作用.



The first test using a simple variable returns the original value assigned ("100") after v1 has been changed to "299". This is the problem.

The second and third test produce the expected results after the variable has been changed to "299".

I do not understand why the first test doesn''t work.

推荐答案

请参阅我对问题的评论.

您需要了解运行时编译时类型. v1编译时类型(从不使用此类名称,请帮自己一个大忙)是System.Object,但是v1 = 100使其 run-time 类型是值类型 int.此外,这是sub,它不返回任何内容.一切按预期进行.如果您可以解释是否还有任何疑问,请提出后续问题(但再次,请参阅我对该问题的评论),然后我将进一步解释.

在CLI中,运行时编译时类型之间的值类型和引用类型之间的边界是一件非常微妙的事情.要了解它,您还需要学习装箱装箱:
http://en.wikipedia.org/wiki/Boxing_%28computer_science%29#Boxing [ ^ ],
http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx [ ^ ].

抱歉,最后引用是C#,而不是VB.NET.首先,没有任何区别. Microsoft可能不会认真对待VB.NET,许多概述和深入的文章仅在C#中可用(也许有些可用,但较难找到).无论如何,如果您想精简.NET,则需要至少了解一些C#,以便能够阅读文章并获得一些帮助和源代码示例……



拳击在这里用VB.NET术语进行了解释:
http://msdn.microsoft.com/en-us/magazine/cc188935.aspx [ ^ ].

归功于找到该文章的OP.

—SA
Please see my comment to the question.

You need to understand run-time and compile-time type. The compile-time type of v1 (never use such names, do yourself a great favor) is System.Object but v1 = 100 makes its run-time type the value type int. Besides, this is sub, it does not return anything. Everything proceeds as expected. If you can explain you concerns if you still have any, please ask a follow-up question (but again, please see my comment to the question), then I''ll explain more.

The boundary between value type and reference type between run-time and compile-time type is a pretty delicate thing in CLI. To understand it, you also need to learn boxing and unboxing:
http://en.wikipedia.org/wiki/Boxing_%28computer_science%29#Boxing[^],
http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx[^].

Sorry that last reference in C#, not VB.NET. First, there is no any difference. Microsoft probably does not take VB.NET seriously, many overviews and in-depth articles are available only in C# (maybe some are available but harder to find). Anyway, if you want to lean .NET, you will need to understand at least some C#, to be able to read articles and get some help and source code samples…



Boxing is explained here in VB.NET terms:
http://msdn.microsoft.com/en-us/magazine/cc188935.aspx[^].

Credit to OP who found the article.

—SA


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

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