Method assignment

A

Alan T

Can I assign a method of a class to a method of another class?

I have a method called ChangeEmployee(int aNewEmployee),
I want to assign this method to another class's method so that the other
class's method to do the same thing.
 
T

Tom Spink

Alan said:
Can I assign a method of a class to a method of another class?

I have a method called ChangeEmployee(int aNewEmployee),
I want to assign this method to another class's method so that the other
class's method to do the same thing.

Hi Alan,

If your classes can be related by inheritance, then one class can inherit
the other:

public class TheBaseClass
{
public void ChangeEmployee ( int aNewEmployee )
{
...
}
}

public class TheDerivedClass : TheBaseClass
{
...
}

Now, you can access ChangeEmployee from an instance of TheDerivedClass, and
indeed any other derived classes of TheBaseClass.

There are a few other methods of minimising code redundancy, but if it's
appropriate to use inheritance, then I suggest taking that approach first.
 
A

Alan T

Hi,

The two classes are not related to each other.
I have embedded one member(listview) of a class inside the other class.
 
P

pr

Hi
If the method is public you can access that method anywhere. or else it
should be static
regs
 
T

Tom Spink

Alan said:
Hi,

The two classes are not related to each other.
I have embedded one member(listview) of a class inside the other class.

Hi Alan,

It's hard to give an appropriate answer without more information about what
it is you need to replicate in the methods. If both methods can perform
the same operation, without requiring an instance of the class, then you
can embed your code in a static method, which you can then call from
anywhere:

///
public static void MyMethod ( )
{
...
}
///

Can you post a code example?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top