Method assignment

  • Thread starter Thread starter Alan T
  • Start date Start date
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.
 
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.
 
Hi,

The two classes are not related to each other.
I have embedded one member(listview) of a class inside the other class.
 
Hi
If the method is public you can access that method anywhere. or else it
should be static
regs
 
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?
 
Back
Top