lalabear

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



Re: Visual C# General how to call a function of another class

Wole Ogunremi

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");
}
}






Re: Visual C# General how to call a function of another class

IsshouFuuraibou

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()





Re: Visual C# General how to call a function of another class

lalabear

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





Re: Visual C# General how to call a function of another class

Mark Dawson

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.