none
how to call a function of another class

    Pregunta

  • hi i am currently trying to call a function of another class.
    (i am not trying to copy the function over i am trying to calling directly to it)

    something like:

    class main
    {
    cow cow1= new cow();
    dog dog1 = new dog();
    }

    i am trying to call for lets say a fucntion called moo from the dog's class fucntion within the dog class.

    class dog
    {
    void woof()
    {
    moo();
    }
    }

    if this was c++ i would have pass the main's instance variable over to both the cow and dog class upon creation but i have no idea how to do this in c# pls help

    viernes, 11 de mayo de 2007 5:49

Respuestas

  • Make sure the class and method scope is accessible to the caller. If both classes are in the same file you don't need to make dog public but you need tomake method woof() either public or internal.

    Code Snippet

    class Main
    {
        cow cow = new cow();
        dog dog = new dog();

        void mainMethod()
        {
           dog.woof();
        }
    }

    public class dog
    {
        public void woof()
        {
           Console.WriteLine("woof woof");
        }
    }


    viernes, 11 de mayo de 2007 6:38
  • Also remember that you can't access a method without a reference to the object or a delegate, the Dog class object doesn't know anything about the cow class, so it can't call "moo()" if it was a method of the cow class.

    Also, you should avoid naming instances the same as your class name, even in examples. the OP was better with dog dog1 = new dog()
    viernes, 11 de mayo de 2007 17:10

Todas las respuestas

  • Make sure the class and method scope is accessible to the caller. If both classes are in the same file you don't need to make dog public but you need tomake method woof() either public or internal.

    Code Snippet

    class Main
    {
        cow cow = new cow();
        dog dog = new dog();

        void mainMethod()
        {
           dog.woof();
        }
    }

    public class dog
    {
        public void woof()
        {
           Console.WriteLine("woof woof");
        }
    }


    viernes, 11 de mayo de 2007 6:38
  • Also remember that you can't access a method without a reference to the object or a delegate, the Dog class object doesn't know anything about the cow class, so it can't call "moo()" if it was a method of the cow class.

    Also, you should avoid naming instances the same as your class name, even in examples. the OP was better with dog dog1 = new dog()
    viernes, 11 de mayo de 2007 17:10
  • hi i am trying delegate but i have been having trouble with an error talking about cant deserialise at the security level i recenrly found a way for it to be solve but the code is in the app.config file

     

    <serverProviders>

                          <formatter ref="soap" typeFilterLevel="Full" />

                      </serverProviders>

     

    how can i add it into my code to cater my program can help me?

    viernes, 11 de mayo de 2007 17:31
  • Hi,

      start a new thread with a different title, so more people can see you are having this issue, since it is not related to the title of the original post.

     

    Mark.

    viernes, 11 de mayo de 2007 19:46
  • hello, 

    can you please specify how to access a method without creating a reference to it's main class  or a delegate?

    sábado, 24 de marzo de 2012 9:21
  • You cannot access a method without an instance unless the method is marked static.

    --
    Mike
    sábado, 24 de marzo de 2012 12:49