将变量发送到另一个类sub:不能引用实例成员

时间:2013-01-24 05:51:05

标签: .net vb.net class variables

我有点难以理解我一直想弄清楚的问题。这是我面临的问题。我有两个类:CustomerFind.vb和CreateInvoice.vb。用户单击搜索按钮以打开另一个最顶层的表单,当用户搜索该客户时,结果随后将显示在DataGridView中。然后,用户为客户选择该行,然后单击“确定”。我需要将(CustomerFind.vb)的CustomersID发送到 - > (CreateInvoice.vb)。一旦我在CreateInvoice中拥有该变量,我需要使用该变量传递给函数以获取客户详细信息的表格...请参阅下面的内容我已经...

这将打开CustomerFind.vb表单...

  Private Sub btnFindCustomer_Click(sender As System.Object, e As System.EventArgs) Handles btnFindCustomer.Click
    Dim findCustomer As New CustomerFind
    findCustomer.ShowDialog()
  End Sub

一旦用户选择他们的客户,变量就会传递给另一个类......

  Public Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
    Dim intCustomerID As Integer = dgvSearchResults.SelectedRows(0).Cells(0).Value

    CreateInvoice.CreateNewInvoice(intCustomerID)

  End Sub

这是CreateInvoice.vb中的子,我也传递了变量......

 Public Shared Sub CreateNewInvoice(ByVal intCustomerID As Integer)

    'Datatable to hold returned results'
    Dim oTable As DataTable
    'Get the data that we need for the customer'
    oTable = CustomerHelper.GetCustomer(intCustomerID)

    txtFirstName.Text = IIf(oTable.Rows(0).Item("First_Name") Is DBNull.Value, "NA", oTable.Rows(0).Item("First_Name")))
    txtLastName.Text = IIf(oTable.Rows(0).Item("Last_Name") Is DBNull.Value, "NA", oTable.Rows(0).Item("Last_Name"))
    txtCity.Text = IIf(oTable.Rows(0).Item("City") Is DBNull.Value, "NA", oTable.Rows(0).Item("City"))
    cboState.SelectedValue = IIf(oTable.Rows(0).Item("State") Is DBNull.Value, "NA", oTable.Rows(0).Item("State"))
    txtAddress.Text = IIf(oTable.Rows(0).Item("Address") Is DBNull.Value, "NA", oTable.Rows(0).Item("Address"))
    txtZipCode.Text = IIf(oTable.Rows(0).Item("Zip_Code") Is DBNull.Value, "NA", oTable.Rows(0).Item("Zip_Code"))
    txtEmail.Text = IIf(oTable.Rows(0).Item("Email") Is DBNull.Value, "NA", oTable.Rows(0).Item("email"))
    txtHomePhone.Text = IIf(oTable.Rows(0).Item("Home_Phone") Is DBNull.Value, "NA", oTable.Rows(0).Item("Home_Phone"))
    txtCellPhone.Text = IIf(oTable.Rows(0).Item("Cell_Phone") Is DBNull.Value, "NA", oTable.Rows(0).Item("Cell_Phone"))
    txtMiddleInitial.Text = IIf(oTable.Rows(0).Item("Middle_Initial") Is DBNull.Value, "NA", oTable.Rows(0).Item("Middle_Initial"))

End Sub

问题在这里存在。所有的文本都不能在共享的方法中参考一个类别的实例成员......我需要从回来的表中填写这些字段。

我不确定我在这里缺少什么,也许我只是累了...

谢谢!

3 个答案:

答案 0 :(得分:1)

我不明白你的期望,但我认为你可能需要这个(将CustomerId从CustomerFind.vb传递给CreateInvoice.vb)。为此,您可以使用以下内容:

创建一个不可见的标签,并将CustomerId分配给CustomerFind.vb中的标签,如

  inlabel.Text=""+CustomerId 

在CreateInvoice.vb

   Dim id as Integer
   id=CustomerFind.inlabel.text

现在您可以使用CustomerId

答案 1 :(得分:0)

自从我做了任何表单开发以来已经有一段时间了,但是你不想做类似的事情:

Public Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
    Dim intCustomerID As Integer = dgvSearchResults.SelectedRows(0).Cells(0).Value
    Dim invoiceDlg as New CreateInvoice

    ' new field in you invoice class
    invoiceDlg.CustomerID = intCustomerID
    ' set up text fields in your dialog init function instead of CreateNewInvoice()
    invoiceDlg.ShowDialog();
End Sub

答案 2 :(得分:0)

好吧,我明白了。我所做的是从保存中调用我的功能来获取客户的详细信息。一旦我拿到它们,我继续从那里设置字段然后关闭表单。一切都填满了。我想说感谢你帮我欣赏它!

由于