如何将参数ByRef传递给VB.net中的表单

时间:2016-04-13 14:16:32

标签: vb.net winforms pass-by-reference

所以我试图将会话变量从我的主窗体(LayoutForm)传递给Connection窗体(ConDevForm)。这是我的两个表单的完整代码。我试图使用@LarsTech给出的解决方案来做到这一点。但我失败了。

我在LayoutForm中创建会话变量,然后通过引用ConDevForm传递它。一旦ConDevForm关闭,我使用LayoutForm中的菜单项与连接的设备进行交互,以验证我通过“会话”控制会话。变量

这有效: 主要表格

Imports NationalInstruments.VisaNS
Imports System.Text
Public Class LayoutForm
    Dim SGFreq As Double = Nothing
    Dim Session As rssmb = Nothing

    Private Sub SMB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMB.Click
        Dim Connect As New ConDevForm(Session)
        Connect.WindowState = FormWindowState.Normal
        Connect.Show()
    End Sub

    Private Sub MSingle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MSingle.Click
        Session.GetRFFrequency(SGFreq)
        SGFreq = SGFreq + 1000000000.0
        Session.confRFFreq(SGFreq, 0, 0, 0)
    End Sub
End Class

连接表格

Imports NationalInstruments.VisaNS
Imports System.Text
Public Class ConDevForm
    Private sesn As rssmb
    Public Sub New(ByRef sesn As rssmb)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        Me.sesn = sesn
        Try
            UseWaitCursor = True
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor
            If sesn Is Nothing Then
                sesn = New rssmb("TCPIP::192.168.138.100::INSTR", 0, 1)
                sesn.confRFFreq(1000 * 1000000.0, 0, 0, 0)
                sesn.confRFLevel(0, 0, 0, 0, 0, 0, 0)
            End If
        Catch ex As System.Runtime.InteropServices.ExternalException
            Dim message As String
            message = "Instrument Status Error: " + ex.Message + Environment.NewLine
            message += "Instrument Error Code: " + ex.ErrorCode.ToString("X")
            MessageBox.Show(message)
        Finally
            System.Windows.Forms.Cursor.Current = Cursors.Default
            UseWaitCursor = False
        End Try
    End Sub

    Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click

    End Sub
End Class

但这不是

Imports NationalInstruments.VisaNS
Imports System.Text
Public Class ConDevForm
    Private sesn As rssmb
    Public Sub New(ByRef sesn As rssmb)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        Me.sesn = sesn
        Try
            UseWaitCursor = True
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor
            If Me.sesn Is Nothing Then
                Me.sesn = New rssmb("TCPIP::192.168.138.100::INSTR", 0, 1)
                Me.sesn.confRFFreq(1000 * 1000000.0, 0, 0, 0)
                Me.sesn.confRFLevel(0, 0, 0, 0, 0, 0, 0)
            End If
        Catch ex As System.Runtime.InteropServices.ExternalException
            Dim message As String
            message = "Instrument Status Error: " + ex.Message + Environment.NewLine
            message += "Instrument Error Code: " + ex.ErrorCode.ToString("X")
            MessageBox.Show(message)
        Finally
            System.Windows.Forms.Cursor.Current = Cursors.Default
            UseWaitCursor = False
        End Try
    End Sub

    Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click

    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

添加变量:

Public Class Form2
  Private sesn As rssmb

  Public Sub New(ByRef sesn As rssmb)
      InitializeComponent()
      Me.sesn = sesn
  End Sub

  Private Sub connect_Click(...) Handles connect.Click
      Me.sesn=connect(...)
  End Sub

如果您创建新表单,则必须重新建立连接。因此,移动引用直到之后你的if-check,你可以在哪里创建一个新版本:

Try
  UseWaitCursor = True
  System.Windows.Forms.Cursor.Current = Cursors.WaitCursor
  If Me.sesn Is Nothing Then
    Me.sesn = New rssmb("TCPIP::192.168.138.100::INSTR", 0, 1)
    Me.sesn.confRFFreq(1000 * 1000000.0, 0, 0, 0)
    Me.sesn.confRFLevel(0, 0, 0, 0, 0, 0, 0)
  End If
  Me.sesn = sesn