delegates that can see members in class

J

Jon Slaughter

Is it possible for delegates to see members in in a class that has the
delegate definition in it?

Basically I want the ability to have users to define there own methods to
incorporate into my class. But instead of having to pass a few fields around
to them when I call them I wonder if there is an easier way for them to see
it?

so I want

class {
delegate
delegate_Collection
call delegates in c ollection
}

some method that gets added to collection in class
{
needs to use a field in class
}



(of course I can pass the field to the method but again I wondering if there
is a better solution)

essentially I want the methods to be as if they were in the class.. well,
they don't need complete unrestricted access and I guess I'll end up having
to pass a reference to the method of the class.

Thanks,
Jon
 
J

Jon Skeet [C# MVP]

Jon Slaughter said:
Is it possible for delegates to see members in in a class that has the
delegate definition in it?

Basically I want the ability to have users to define there own methods to
incorporate into my class. But instead of having to pass a few fields around
to them when I call them I wonder if there is an easier way for them to see
it?

so I want

class {
delegate
delegate_Collection
call delegates in c ollection
}

some method that gets added to collection in class
{
needs to use a field in class
}



(of course I can pass the field to the method but again I wondering if there
is a better solution)

essentially I want the methods to be as if they were in the class.. well,
they don't need complete unrestricted access and I guess I'll end up having
to pass a reference to the method of the class.

I'm afraid I'm still completely lost as to what you want. Why don't you
just pass a reference to the instance of the class itself?
 
G

Gilles Kohl [MVP]

Is it possible for delegates to see members in in a class that has the
delegate definition in it?

Basically I want the ability to have users to define there own methods to
incorporate into my class. But instead of having to pass a few fields around
to them when I call them I wonder if there is an easier way for them to see
it?

so I want

class {
delegate
delegate_Collection
call delegates in c ollection
}

some method that gets added to collection in class
{
needs to use a field in class
}



(of course I can pass the field to the method but again I wondering if there
is a better solution)

essentially I want the methods to be as if they were in the class.. well,
they don't need complete unrestricted access and I guess I'll end up having
to pass a reference to the method of the class.

Thanks,
Jon

Hmm, try to add your delegates to the list with

m_ListOfDelegates.Add(this.MethodName)?

For that, the method should not be static and a member of your class.

Here's a (made up) example of a class that performs a chain of math
operations. Each operation gets a double, does something to it, and returns
it. In addition, it prints some diagnostic info if a boolean member of the
class tells it to do so. (This is the example of fields inside the class
getting accessed by the delegates)


using System;
using System.Collections.Generic;

class MathOps
{
public delegate double Operation(double argument);

List<Operation> m_Operations;

bool m_TraceCalls;

double Negate(double value)
{
double result = -value;
if(m_TraceCalls)
{
Console.WriteLine("Negate called with {0}, returning {1}",
value, result);
}
return result;
}

double Invert(double value)
{
double result = 1 / value;
if(m_TraceCalls)
{
Console.WriteLine("Invert called with {0}, returning {1}",
value, result);
}
return result;
}

public MathOps(bool traceCalls)
{
m_TraceCalls = traceCalls;
m_Operations = new List<Operation>();
m_Operations.Add(new Operation(this.Negate));
m_Operations.Add(new Operation(this.Invert));
}

public MathOps() : this(false) { }

public double PerformOperations(double value)
{
foreach(Operation op in m_Operations)
{
value = op(value);
}
return value;
}
}

class Test
{
static void Main(string[] args)
{
MathOps mathOps = new MathOps(true);

Console.WriteLine("Result: {0}", mathOps.PerformOperations(-81));
Console.WriteLine("Press a key to continue");
Console.ReadKey();
}
}

Regards,
Gilles.
 

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