Calling Public Sub works in some places but not others. VB.net 2010

不羁岁月 提交于 2019-12-12 21:17:31

问题


I am having a problem calling a public sub from another form in my current form. The form that I am trying to call a public sub from is open when trying to make the call. I have used this method of calling public subs in this application, and they have always worked in the past. But now I am getting the following error...

Error 1 Reference to a non-shared member requires an object reference.

Here is the basic run down.... I have a main form (frmWorkOrdersMain) that is a work order system. My main form opens a sub form (frmWorkOrder) that handles individual work orders. To add parts to frmWorkOrder. frmWorkOrder opens a sub form (Form1). Form1 constructs a list of parts in an arraylist, and i want to pass this list to a public sub in frmWorkOrder. But i get the error listed above.

Now I refresh certain DGVs in frmWorkOrdersMain from frmWorkOrder perfectly fine using a public sub in frmWorkOrderMain.

Public Sub in frmWorkOrderMain...

Public Sub ReloadWorkOrdersAndJobs()
    Dim DS1 As New DataSet
    Dim DS2 As New DataSet
    Dim DB As New DBWrapper

    DS1 = DB.GetCustWO(CInt(cboWOCust.SelectedValue))
    DS2 = DB.GetCustJobs(CInt(cboWOCust.SelectedValue))

    dgvWO2.DataSource = DS1.Tables("WorkOrders")
    dgvJobs2.DataSource = DS2.Tables("Jobs")
    dgvWO2.Columns(0).Width = 90
    dgvWO2.Columns(1).Width = 493
    dgvWO2.Columns(2).Width = 85
    dgvWO2.Columns(3).Width = 85
    dgvJobs2.Columns(0).Width = 80
    dgvJobs2.Columns(1).Width = 353
    dgvJobs2.Columns(2).Width = 80
    dgvJobs2.Columns(3).Width = 80
    dgvJobs2.Columns(4).Width = 80
    dgvJobs2.Columns(5).Width = 80

    WOMainView()
End Sub

Call in frmWorkOrder...

Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles tsbSaveWO.Click
   (...code...)
   frmDMSWorkOrders.ReloadWorkOrdersAndJobs()
End Sub

That works fine all the time. I can call that from any form in my application. Now here is my public sub that wont work

Public Sub in frmWorkOrder...

Public Sub AddParts(ByVal PartList As ArrayList)
    Dim P As New ArrayList
    P = PartList
    (...CODE....)
End Sub

Here is the call from form1...

Private Sub btnSaveParts_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveParts.Click
    Dim I As Integer
    Dim PartList As New ArrayList
    Dim P As Part

    For I = 0 To dgvPartsToBeAdded.RowCount - 1
        P = New Part
        P.ID = CInt(dgvPartsToBeAdded.Rows(I).Cells(0).Value)
        P.Brand = CStr(dgvPartsToBeAdded.Rows(I).Cells(1).Value)
        P.ModelNum = CStr(dgvPartsToBeAdded.Rows(I).Cells(2).Value)
        P.PClass = CInt(dgvPartsToBeAdded.Rows(I).Cells(7).Value)
        P.UPC = CStr(dgvPartsToBeAdded.Rows(I).Cells(4).Value)
        P.Description = CStr(dgvPartsToBeAdded.Rows(I).Cells(3).Value)
        P.Serial = CStr(dgvPartsToBeAdded.Rows(I).Cells(5).Value)
        P.Notes = CStr(dgvPartsToBeAdded.Rows(I).Cells(8).Value)
        P.ServiceTag = CStr(dgvPartsToBeAdded.Rows(I).Cells(6).Value)

        PartList.Add(P)
    Next

    frmWorkOrder.AddParts(PartList)

End Sub

Its just like using the first public sub as far as I can see, but i cant seem to resolve the error that i am receiving...

Error 1 Reference to a non-shared member requires an object reference.

Thank you for any help, guidance, or direction in advance. Dan


回答1:


The the situation is exactly as the error message states. The form is a class so you need to have an instance to call its methods.

Public Class TestCl
     Public Sub Test
     End Sub
End Class

TestCl.Test() .. error

Dim tC as New TestCl

tC.Test() .. OK

VB6 has the concept of creating an instance of the form automatically but as far as I know it is not the case of .net.



来源:https://stackoverflow.com/questions/17949891/calling-public-sub-works-in-some-places-but-not-others-vb-net-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!