none
Call Method in another Class

    Question

  • Hi,

    I have 3 projects...an executable project - main.exe, and 2 class librarys - classLibA, classLibB.
    The executable project references the 2 class librarys. An instance of each class is created in the executable.

    When classLibA is called I want it to call a method in classLibB.
    How do I do this without having to create another instance of classLibB?

    Thanks.

    Wednesday, July 21, 2010 8:51 AM

Answers

  • Why dont you create classLibB first in the exe and pass that instance to classLibA's constructor when creating it
    • Proposed as answer by Louis.fr Wednesday, July 21, 2010 5:08 PM
    • Marked as answer by Harry Zhu Wednesday, July 28, 2010 7:39 AM
    Wednesday, July 21, 2010 9:01 AM
  • Agree with tribal. To respect OOP, you wouldn't instance the two classes A and B in a third classC that has to know the order in which to call the classA and classB. The responsibility of a method is on the belonging class. If a class calls another it's because it wants to ask something and maybe getting something back. The triangle usually is not a valid geometry in object oriented world.

    If classB do something useful to classA then only classA should use it, internally in its methods, and know of its existence. Instances can be done inside the method or in the constructor in the factory if you need to pass reference to a living object. Privilege encapsulation, cohesion, and low coupling when doing your choices.

    Giuseppe

    • Marked as answer by Harry Zhu Wednesday, July 28, 2010 7:40 AM
    Wednesday, July 21, 2010 7:54 PM

All replies

  • hi

    If in order for you  to call a method in ClassB without creating an instance,

    you need to declare that method as Static.

    Wednesday, July 21, 2010 9:00 AM
  • Why dont you create classLibB first in the exe and pass that instance to classLibA's constructor when creating it
    • Proposed as answer by Louis.fr Wednesday, July 21, 2010 5:08 PM
    • Marked as answer by Harry Zhu Wednesday, July 28, 2010 7:39 AM
    Wednesday, July 21, 2010 9:01 AM
  • Depends on classLibB library, if the methods in classLibB are static then not need of creating any instance you directly refer the methods.

    Check whether methods are static.


    Nanda - Misys Software Solutions,Bangalore
    Wednesday, July 21, 2010 9:18 AM
  • Agree with tribal. To respect OOP, you wouldn't instance the two classes A and B in a third classC that has to know the order in which to call the classA and classB. The responsibility of a method is on the belonging class. If a class calls another it's because it wants to ask something and maybe getting something back. The triangle usually is not a valid geometry in object oriented world.

    If classB do something useful to classA then only classA should use it, internally in its methods, and know of its existence. Instances can be done inside the method or in the constructor in the factory if you need to pass reference to a living object. Privilege encapsulation, cohesion, and low coupling when doing your choices.

    Giuseppe

    • Marked as answer by Harry Zhu Wednesday, July 28, 2010 7:40 AM
    Wednesday, July 21, 2010 7:54 PM
  • define the funcation static
    好女孩上天堂,坏女孩走四方。。。
    Thursday, July 22, 2010 2:05 AM