Results 1 to 5 of 5

Thread: [RESOLVED] Class Structure Issue

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    Resolved [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?

  2. #2
    New Member
    Join Date
    May 2006
    Posts
    7

    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:
    1. Class Main
    2.  
    3.     Class Child
    4.         Public Sub DoSomething()
    5.             SendMessage()
    6.         End Sub
    7.     End Class
    8.  
    9.     Public Shared Sub SendMessage()
    10.         '
    11.     End Sub
    12. End Class

    The key here is to declare SendMessage as Public and Shared.

  3. #3
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Class Structure Issue

    How about making the Poll method a member of the motor class instead of the inputs class?

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    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?

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width