Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.70/5 (3 votes)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 16-Dec-13 22:17pm    
As I already mentioned, it would be interesting to know why did you change this part of code. What it was before? Is it about using some
members of a form or other control class outside of this class?
Oh... you did it wrong. "DoEvents"... For serial communication, you should rather use threading.
—SA
nca8503 16-Dec-13 22:25pm    
i need to change this project to dll since i need to put this dll into another projectA.means I have another big projectA(window application form) that have button when user clicked, this dll project (exe program) will popup).I know i can call using shell the exe without convert to dll, how ever user want to have a control in this exe from projectA.
Sergey Alexandrovich Kryukov 16-Dec-13 22:37pm    
I think I am asking your for the third time already, and you don't answer. Change to DLL cannot create this problem. I know why changing code to DLL and provided you with the detailed advice on this work. If your code compiled before porting it to DLL, it should compile after. If it does not compile, it means that you screw up something in the code.

How can you expect help if you ignore questions yourself?

—SA
Sergey Alexandrovich Kryukov 16-Dec-13 22:20pm    
And please comment the line of code where the compilation error is shown.
What is Sensor1: class, structure, or instance of some type. Where is the declaration of ButtonSend?
—SA
nca8503 16-Dec-13 23:13pm    
i'm not changing this code. the code is still same.i only convert by change type project to class library. and i also has said before.before i convert into class library it was successfully build and run. Sensor1 is class.i already put the code above (Public Class Sensor1). also write on question above which line the error occured. (This error occur on the module in line Sensor1.Button_Send.Enabled = True and Sensor1.Button_Disconnect.Enabled = True.)

"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.
 
Share this answer
 
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
VB
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
 
Share this answer
 
Comments
Sean Donnahoe 5-Jan-16 16:13pm    
Sergey, while I understand the validity of your point, it is not always as convenient as that. Sometimes, it is necessary to write an application that is needed for an individual's work flow and needs to be done in a hurry. We don't all have the time to sit back and learn, step 1, step 2, step 3. That is why this community exists, to help those of us in this very situation. In our spare time, I agree, we should go back and do they steps as you suggest, but when someone needs a fix quickly, they come to forums like this, to get real help, not be berated or belittled by senior members.

I understand English may not be your first language, but sometimes your comments can come off as very insulting to those of us that speak it natively.
Sergey Alexandrovich Kryukov 5-Jan-16 16:28pm    
Thank you for your comment, Sean.

I don't see your point. Are you saying that a person who does not understand what to do still do some useful job? Sure, sometimes. But does it need our advice? Why? I don't think so. It's not just about what to do in practice. I some question is based on misunderstanding or lack of knowledge, the advice to learn would be quite adequate, and the idea not to learn be be safely left out of the brackets. The choice is not symmetric.

Also, it's not clear what is "not always convenient as that"? Did I mention anything related to "convenience"? What seems to be "convenient" or "inconvenient"?

Speaking about "very insulting", I would suggest you give exact arguments, instead of some claims. It's especially unclear how it could be related to language knowledge/experience. My naive view is: if something feel offended by my judgement, this is only about my judgement/opinion itself, not about the way I express it. Perhaps my expression are usually softer than my real thoughts. If you want to say that my opinion is offensive, discuss my opinion, otherwise please explain what exactly expression you mean.

On this particular topic, my opinion is: we already have way too many people writing code without understanding what they do, so it's better not to encourage it.

Thank you.

—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:

VB
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:

VB
Dim S1 as Sensor1 = new Sensor1
ConnectPort(S1)



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

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


Hopefully thats a bit more of a helpful solution than 'quit programming you suck'
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900