vb.net 错误:“对非共享成员的引用需要对象引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18538684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:52:34  来源:igfitidea点击:

Error: "Reference to a non-shared member requires an object reference"

vb.netvisual-studio-2012

提问by BSanders

I am building a small app that is pulling script from within an object. I'm down to the portion where my code is pulling back the field from the object that has the script and I'm getting this error.

我正在构建一个从对象中提取脚本的小应用程序。我回到了我的代码从具有脚本的对象中拉回字段的部分,我收到了这个错误。

"reference to a non-shared member requires an object reference"

“对非共享成员的引用需要对象引用”

I'm not sure what to change or how to get around this. Does anyone out there have any suggestions?

我不确定要改变什么或如何解决这个问题。有没有人有任何建议?

Here is the code that i have so far. It's a simple app that has a combobox that you choose the company from and on button click it will get the script and show it in the textbox.

这是我到目前为止的代码。这是一个简单的应用程序,它有一个组合框,您可以从中选择公司,然后单击按钮,它将获取脚本并将其显示在文本框中。

Here is my code:

这是我的代码:

Imports System.IO
Public Class Form1
    Public M3System As MILLSYSTEMLib.System
    Public M3Script As MILLCOMPANYLib.CScripting

    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'On Error Resume Next
        Try
            Dim Approved As Integer

            ' Create a Millennium system obects
            M3System = CreateObject("MillSystem.System")
            M3System.Load("Millennium")

            'run login script
            Dim User As Object = M3System.Login()

            ' See if login worked
            If User Is Nothing Then
                'MsgBox("Login failed!")
                Approved = 0
            Else
                'MsgBox("Login successful")
                'if approved=1 then the user is able to access M3
                Approved = 1
            End If

            'populate combo box
            For Each Company In M3System.Companies
                cb_COID.Items.Add(Company.Field("co").ToString)
            Next

        Catch ex As Exception
            Me.Close()
        End Try


    End Sub

    Public Sub btn_LoadScript_Click(sender As Object, e As EventArgs) Handles btn_LoadScript.Click

        Dim CoCode As String = cb_COID.SelectedItem
        Dim script As String = M3Script.vbscript

        If IsNothing(cb_COID) Then
            MessageBox.Show("Select a Company Code")
        End If
        For Each CoCode In M3Script.co
            tb_Script.Text = script
        Next

    End Sub

回答by Nicholas Post

I'm guessing that the line you are failing on is Dim script As String = M3Script.vbscript

我猜你失败的线路是 Dim script As String = M3Script.vbscript

If that is the case, it's because you are declaring the M3Script, but you are not creating an instance of it.

如果是这种情况,那是因为您声明了M3Script,但没有创建它的实例。

Try setting the object somewhere by adding M3Script = new MILLCOMPANYLib.CScriptingto your code, or however you would load it (perhaps M3Script = CreateObject("MillSystem.Script")?)

尝试通过添加M3Script = new MILLCOMPANYLib.CScripting到代码中来将对象设置在某处,或者您会加载它(也许M3Script = CreateObject("MillSystem.Script")?)