在表单中的其他地方引用变量和对象 [英] Reference variables and objects elsewhere in a form

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

问题描述

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

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 在表单(或模块或其他任何东西)中被提及的事实是偶然的.虽然构造函数在几个方面确实很特殊,但在作用域方面,它只是另一个过程.

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

在表单级别声明,它具有表单/类级别的范围.您现在可以在表单中的任何位置引用 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 变量,它与表单无关/同名的类级别变量.本地版本掩盖了另一个.对此的部分困惑似乎是有些人认为他们需要(重新)使用 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 语句创建了一个 块作用域(For Each/Next, If/End IfUsing/End Using).在块内声明的变量的范围仅限于该块.基本上,(几乎)任何导致缩进的东西都会创建一个块范围.

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 可以在过程中的其他地方使用:它的作用域已从 Block 更改为 Local.

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

有关详细信息,请参阅 MSDN:
- Visual Basic 中的范围
- 值类型与引用类型

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

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

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