错误 - 对非共享成员的引用需要对象引用。 [英] Error -Reference to a non-shared member requires an object reference.

查看:136
本文介绍了错误 - 对非共享成员的引用需要对象引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将窗口应用程序转换为类库后出现此错误。在此之前,它已成功运行。我的窗口申请表有一个模块。在Sensor1.Button_Send.Enabled = True和Sensor1.Button_Disconnect.Enabled = True的模块上发生此错误。



请帮助我或给我一些想法,因为我'表格:

公共类传感器1 

公共子Button_Send_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button_Send.Click

If(ComPort.IsOpen = True)Then

Application.DoEvents()

否则
MsgBox(COM端口未打开。连接到COM端口再试一次。)
结束如果


End Sub

Public Sub Button_Disconnect_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button_Disconnect.Click

Flag_Disconnect = True

Application.DoEvents()

ComPort.Close()
Me.Button_Connect.Enabled = True
Me.ComboBox1。 Enabled = True
Me.Button_Disconnect.Enabled = False
Me.Button_Send.Enabled = False

Me.ComboBox1.Items.Clear()

结束次级
结束等级





模块:

进口系统。 IO 
Imports System.Data

模块Module1

Sub ConnectPort()

尝试
ComPort = My.Computer。 Ports.OpenSerialPort(PortNm)

ComPort.BaudRate = 9600
ComPort.StopBits = 1
ComPort.Parity = 0
ComPort.DataBits = 8


Sensor1.Button_Send.Enabled = True
Sensor1.Button_Disconnect.Enabled = True

Catch ex As Exception
MsgBox(COM port:& amp; ; PortNm& 使用。选择一个可用的端口,然后重试。)
返回
结束尝试

结束次级

结束模块

解决方案

不可能这样。我坚持:

怎么办?当然,不要开发UI应用程序或者通信,或任何先进的东西。



这是非常粗鲁和居高临下的,特别是因为他描述的行为确实发生了。



Visual Basic项目具有表单默认实例的概念:

http://stackoverflow.com/questions/4698538/there-is-a-default-instance-of -form-in-vb-net-but-not-in-c-why [ ^ ]



所以,如果你转换从Windows窗体应用程序到类库的项目会丢失该行为,并且根据现有代码是否使用该默认实例,您可能会出现以下错误:



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



我还应该提到在转换为类库时可能会收到其他错误:

'结束'语句不能在类库项目中使用

http://msdn.microsoft.com/en-us/library/686w7tx3%28v= vs.90%29.aspx



所以要说明在将项目转换为类库时,它将完全相同的编译......简直就是假的。 / blockquote>

请参阅: http://msdn.microsoft.com/en -us / library / zc2b427x.aspx [ ^ ]。



Th e类型成员可以共享或不共享。那些不共享的那些被称为实例成员,因为它们与一个类型的实例相关,而不是一般的类型(在上面引用的MSDN库中使用的相当尴尬的措辞)。具有这种成员的类型的不同实例通常具有这些成员的不同值(或引用)。假设您有一个类 MyClass ,带有整数字段或(读/写)属性 MyInstanceMember 并正确共享或字段 MySharedMember 。然后

  MyClass  .MyInstanceMember =  3  ' 无效 
' 但是
MyClass .MySharedMember = 4 ' 有效





怎么办?当然,不要开发UI应用程序,通信或任何高级技术。你还没有准备好。相反,将它放在一边,并尝试至少学习.NET,语言和OOP的基础知识。类型与实例,值类型与引用类型,引用,常量,变量和成员,属性,委托和事件,所有这些。最终,OOP,虚拟方法,后期绑定,多态,接口,它们的声明,实现和使用。在基于简单的仅限控制台的应用程序进行练习时,最好这样做。如果你像往常一样跳到实际工作中,你只会浪费太多时间而不会获得真正的体验。非常认真。



-SA


我同意第一个解决方案是无益的,来了居高临下。该问题的基本解决方案是将实例化引用传递给您在模块调用中引用的父类,并使用该实例启用或禁用按钮。



这看起来像这样:



  Sub  ConnectPort ( ByRef  c1  as  Sensor1)

尝试
ComPort = My.Computer.Ports.OpenSerialPort(PortNm)

ComPort.BaudRate = 9600
ComPort.StopBits = 1
ComPort.Parity = 0
ComPort.DataBits = 8


c1.Button_Send.Enabled = True
c1.Button_Disconnect.Enabled = True

Catch ex As 异常
MsgBox( COM端口:& PortNm&使用 。选择一个可用的端口,然后重试。
返回
结束 尝试

结束 Sub







如果您从Sensor1类本身打电话,您可以这样打电话:



ConnectPort(我)



或者如果从类库外部调用,则需要首先实例化Sensor1类



例如:



  Dim  S1  as  Sensor1 =  new 传感器1 
ConnectPort(S1)







您可能希望首先显示s1类对象,如果它是一个表单。



  Dim  S1  as  Sensor1 =  new 传感器1 
S1.show()
ConnectPort(S1)





希望这比退出编程你好更有帮助的解决方案


Hi,
I get this error after I converted my window application into class library. Before that, it was successfully run. My window application form have a module. This error occur on the module in line Sensor1.Button_Send.Enabled = True and Sensor1.Button_Disconnect.Enabled = True.

Please help me or give some idea as i'm new in vb.

Form:

Public Class Sensor1

Public Sub Button_Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Send.Click

                If (ComPort.IsOpen = True) Then
            
                Application.DoEvents()

        Else
            MsgBox("COM port is not open.  Connect to a COM port and try again.")
        End If


    End Sub

 Public Sub Button_Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Disconnect.Click

        Flag_Disconnect = True

        Application.DoEvents()

        ComPort.Close()
        Me.Button_Connect.Enabled = True
        Me.ComboBox1.Enabled = True
        Me.Button_Disconnect.Enabled = False
        Me.Button_Send.Enabled = False

        Me.ComboBox1.Items.Clear()
        
    End Sub
End Class



Module:

Imports System.IO
Imports System.Data

Module Module1

 Sub ConnectPort()
        
        Try
            ComPort = My.Computer.Ports.OpenSerialPort(PortNm)

            ComPort.BaudRate = 9600     
            ComPort.StopBits = 1 
            ComPort.Parity = 0      
            ComPort.DataBits = 8

            
            Sensor1.Button_Send.Enabled = True
            Sensor1.Button_Disconnect.Enabled = True

        Catch ex As Exception
            MsgBox("COM port: " & PortNm & " is used . Select an available port and try again.")
            Return
        End Try

    End Sub

End Module

解决方案

"It cannot be so. I insist:"
"What to do? Quite certainly, not to develop UI applications, or communications, or anything advanced."

This is extremely rude and condescending, especially because the behavior he describes does happen.

Visual Basic projects have the concept of a default instance of a form:
http://stackoverflow.com/questions/4698538/there-is-a-default-instance-of-form-in-vb-net-but-not-in-c-why[^]

So if you convert the project from a Windows Form application to a class library you lose that behavior and depending on whether the existing code utilizes that default instance, you can have errors such as:

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

I should also mention that you may receive other errors when converting to a class library such as this one:
"'End' statement cannot be used in class library projects"
http://msdn.microsoft.com/en-us/library/686w7tx3%28v=vs.90%29.aspx

So to state that when converting a project to a class library that it will compile exactly the same... is simply false.


Please see: http://msdn.microsoft.com/en-us/library/zc2b427x.aspx[^].

The type members can be shared or not. Those which are not shared are called instance members, because they a related to an instance of a type, not a type "at large" (in the pretty awkward phrasing used in the MSDN library referenced above). Different instances of the type with such members generally have different values (or referenced) of such members. Let's say you have a class MyClass with integer field or (read/write) property MyInstanceMember and Shared properly or field MySharedMember. Then

MyClass.MyInstanceMember = 3 ' is invalid
' but
MyClass.MySharedMember = 4 ' is valid



What to do? Quite certainly, not to develop UI applications, or communications, or anything advanced. You are not ready yet. Instead, set it all aside and try to learn at least the basics of .NET, the language and OOP. Types versus instances, value types versus reference types, references, constants, variables and members, properties, delegates and events, all that stuff. And ultimately, OOP, up to virtual methods, late binding, polymorphism, interfaces, their declaration, implementation and use. It's the best to do it while doing exercises based on simple console-only application. If you jump right to real work, as you already did, you will only waste too much time and won't get real experience. Very seriously.

—SA


I concur that the first solution was rather unhelpful and came across condescending. A basic solution to the issue would be to pass an instanced reference to the parent class you're referring to in the module call and using that instance to enable or disable the buttons.

This might look something like this:

Sub ConnectPort(ByRef c1 as Sensor1)

 Try
            ComPort = My.Computer.Ports.OpenSerialPort(PortNm)
 
            ComPort.BaudRate = 9600     
            ComPort.StopBits = 1 
            ComPort.Parity = 0      
            ComPort.DataBits = 8
 
            
            c1.Button_Send.Enabled = True
            c1.Button_Disconnect.Enabled = True
 
        Catch ex As Exception
            MsgBox("COM port: " & PortNm & " is used . Select an available port and try again.")
            Return
        End Try
 
    End Sub




If you were calling it from Sensor1 class itself you would do the call like so:

ConnectPort(me)

or if calling from outside the class library you would need to instantiate the Sensor1 class first

for instance:

Dim S1 as Sensor1 = new Sensor1
ConnectPort(S1)




You might want to 'show' the s1 class object first as well if its a form.

Dim S1 as Sensor1 = new Sensor1
S1.show()
ConnectPort(S1)



Hopefully thats a bit more of a helpful solution than 'quit programming you suck'


这篇关于错误 - 对非共享成员的引用需要对象引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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