[RESOLVED] Class Structure Issue
I am using Visual Studio 2005. I have programmed a class for a motor controller. A method of the motor controller class is SendMessage, which sends a string to the motor controller. The motor class has a collection of another class - digital inputs to the motor controller. To access the status of a particular input looks something like this: clsMotor.Inputs(1).Status. I want to be able to poll the inputs like such: clsMotor.Inputs.Poll. However, the Poll method in the Inputs collection needs to use the SendMessage method of clsMotor. But there is not an instance of clsMotor in my Inputs collection - thus a problem.
What's the best way to fix this problem?
Re: Class Structure Issue
If I understand this correctly, I too have a similar class structure in one of my programs. Like so:
VB Code:
Class Main
Class Child
Public Sub DoSomething()
SendMessage()
End Sub
End Class
Public Shared Sub SendMessage()
'
End Sub
End Class
The key here is to declare SendMessage as Public and Shared.
Re: Class Structure Issue
How about making the Poll method a member of the motor class instead of the inputs class?
Re: Class Structure Issue
Thanks for the suggestions. I can't make the SendMessage routine Public Shared because the routine uses an instance of another class. The specific error I receive is "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."
Second suggestion: Maybe that's the only way to resolve this. It seems a little counter-intuitive to place the poll routine in the motor class. Plus, I will run into another problem when I try to set the status of a specific output.
Are there any other suggestions?
Re: Class Structure Issue
The error message could be fixed by changing the private class object from "Private minstSerialComm as clsSerialCommunication" to "Private Shared minstSerialComm as clsSerialCommunication"
Thanks for all the input.