Refering the method of one class file to an another class file in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalpana84
    New Member
    • Mar 2008
    • 1

    Refering the method of one class file to an another class file in c#

    hi,

    Am doing coding in c#,... actually i am creating four different classes in four different . And all classes has the same namespace. What to be done
    If i want invoke the method of one class file from an another class file..

    i just created a object of another class in the present class but am unable to invoke the method.........

    could u plz help me............. ..
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by kalpana84
    hi,

    Am doing coding in c#,... actually i am creating four different classes in four different . And all classes has the same namespace. What to be done
    If i want invoke the method of one class file from an another class file..

    i just created a object of another class in the present class but am unable to invoke the method.........

    could u plz help me............. ..
    Have you set the access type to public for the method you are trying to call?

    Class1 calls method in Class2

    Code:
     obj object1 = Class2.Method1();
    Class2

    Code:
     public object Method1() 
    {
    return new object;
    }
    Give this a try

    Nathan

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by nateraaaa
      Have you set the access type to public for the method you are trying to call?

      Class1 calls method in Class2

      Code:
       obj object1 = Class2.Method1();
      Class2

      Code:
       public object Method1() 
      {
      return new object;
      }
      Give this a try

      Nathan
      All you have to do is create an instance of the object that you need to use and call it's public methods.
      -Frinny

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by nateraaaa
        Have you set the access type to public for the method you are trying to call?

        Class1 calls method in Class2

        Code:
         obj object1 = Class2.Method1();
        Class2

        Code:
         public object Method1() 
        {
        return new object;
        }
        Give this a try

        Nathan
        Not quite. To access a method using class name alone requires that the method be static. To access instance methods, you need an instance of that class. The public modifier controls access across namespaces.

        Comment

        Working...