引用变量和其他形式的对象 [英] Reference variables and objects elsewhere in a form

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

问题描述

我试图将设备对象传递给表单对象,然后在表单按钮上的click事件中使用该设备对象。但是我不知道如何在按钮事件中正确引用设备对象。

I am trying to pass an equipment object to a form object and then use that equipment object in a click event from a button on the form. But I don't know how to properly reference the equipment object within the button event.

我使用以下方法设置新的表单实例:

I set up the new form instance using:

Public Sub New(ByRef thisEquip As classEquipment)
    Me.InitializeComponent()
    Me.Text = thisEquip.equipName & " Tests"
End Sub

并设置按钮单击事件,如下所示:

and set up the button click event like this:

Private Sub btnUpdateAndClose_Click(sender As Object, e As EventArgs) Handles btnUpdateAndClose.Click
    Call updateTestList(thisEquip)
End Sub

但是无法识别 thisEquip对象。我认为这是因为发件人是按钮,而不是表单本身。但是,我不知道如何从表单中引用设备对象。

But the 'thisEquip' object is not recognized. I think this is because the sender is the button and not the form itself. However, I don't know how to reference the equipment object from the form.

推荐答案

作用域取决于声明变量的位置。您可能会错过一些忽略链接的内容-每个作用域级别的摘要都包括在其中声明了的短语。

The Scope depends on where a variable is declared. You might have missed something skimming the link - each scope level summary includes the phrase in which it is declared.

现在看一下您的构造函数:

Now look at your constructor:

Public Sub New(ByRef thisEquip As classEquipment)

thisEquip 声明作为构造函数的参数。因此,它仅存在于该过程中。该过程采用某种形式,或者在该形式(或模块或其他任何形式)中提及 thisEquip 的事实是偶然的。虽然构造函数确实在几种方面是特殊的,但就 Scope 而言,它只是另一个过程。

thisEquip is declared as an argument to the constructor. Thus, it only exists in that procedure. The fact that the procedure is in a form or that thisEquip is mentioned in the form (or module or anything else) is incidental. While it is true that the constructor is special in several ways, in matters of Scope, it is just another procedure.

要保存对它的引用以在其他地方使用:

To save a reference to it for use elsewhere:

Public Class Form1
    ' declare a variable to hold the reference
    Private myEquip As classEquipment
    ' declare an array
    Private myImgs As Image()

    Public Sub New(ByRef thisEquip As classEquipment)
        InitializeComponent()
        ...
        myEquip = thisEquip         ' assign param to the var

        ' assign array of images to the Form level var
        ' via a temp array
        myImgs = New Image() {My.Resources.add, 
                              My.Resources.ballblack, My.Resources.ballblue,
                              My.Resources.ballgreen}
    End Sub

已声明,格式为l,它具有表单/类级别的范围。现在,您可以在表单中的任何位置引用 myEquip 或myImgs。 请勿使用 Dim ,仅在向表单级对象分配内容时会创建一个新的本地变量,但名称相同。

Declared at the form level, it has form/class level scope. You can now reference myEquip or myImgs anywhere in the form. Do Not Use Dim when merely assigning something to a form level object - it will create a new local, but identically named variable.

其他常见范围级别:

Private myFoo as Int32

Private Sub DoSomething()
    Dim myBar As String 
    myBar = "Ziggy"
    ...
    Dim myFoo As Int32 = 7
End Sub

这通常称为本地范围。我使用的是过程级别,因为它与其他术语进行比较和对比更好。

This is more often called local scope. I am using procedure level because it compares and contrasts better to the other terms.

myBar DoSomething 方法中声明,因此它具有过程级范围-仅存在于该方法中。尝试在其他地方使用它会导致错误。这类似于上面的构造函数示例,主要区别在于 thisEquip 对象作为参数传递,而不是在本地声明。

myBar is declared in the DoSomething method, so it has procedure level scope - it only exists in that method. Trying to use it elsewhere will result in an error. This is similar to the constructor example above with the main difference being that the thisEquip object was passed as a parameter rather than declared locally.

这会引起一些困惑:方法中的 Dim myFoo 声明(创建!)一个新的,仅限本地的 myFoo 变量,与同名的Form / Class级别变量无关。本地版本不显示其他版本。造成这种混乱的部分原因似乎是,有些人认为他们需要(重新)使用 Dim 才能使用变量。

This leads some to get confused: the Dim myFoo in the method declares (creates!) a new, local-only myFoo variable which has no relation to the Form/Class level variable of the same name. The local version shadows out the other. Part of the confusion for this seems to be that some think they need to (re) use Dim before they can use a variable. You do not.

直接来自MSDN:

If n < 1291 Then
    Dim cube As Integer
    cube = n ^ 3
End If

相当数量的VB语句创建了块作用域对于每个 / 下一步 If / 结束If 使用 / 最终使用)。在块内声明的变量的作用域仅限于该块。基本上,(几乎)任何会导致缩进的东西都会创建一个块作用域。

A fair number of VB statements create a block scope (For Each/Next, If/End If and Using/End Using). Variables declared inside a Block, have a scope limited to that block. Basically, (almost) anything which results in indentation creates a Block Scope.

Private Sub .....
    Dim cube As Int32

    If n < 1291 Then
       cube = n ^ 3
    End If

现在, cube 可以在过程的其他地方使用:其范围已从块更改为本地。

Now, cube can be used elsewhere in the procedure: its scope has been changed from Block to Local.

有关更多详细信息,请参见MSDN:

- Visual Basic中的作用域 < br>
-值类型与引用类型

For more details, see MSDN:
- Scope In Visual Basic
- Value Types vs Reference Types

这篇关于引用变量和其他形式的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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