-
April 15th, 2010, 09:30 AM
#1
Cannot create/invoke delegate
TcpClient.cs
Code:
namespace TCP_Communication
{
class TcpClient
{
// ...
public delegate void OnDataAvailable(char[] data);
// ...
}
}
Program.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace TCP_Communication
{
class Program
{
static void Main(string[] args)
{
// ...
Client.OnDataAvailable = DataArrived;
// ...
}
public void DataArrived(char[] newdata)
{
// empty for now
}
}
}
Error:
'OnDataAvailable': cannot reference a type through an expression; try 'TCP_Communication.TcpClient.OnDataAvailable' instead
Yes, I tried 'TCP_Communication.TcpClient.OnDataAvailable' instead even though it is obviously wrong:
Program.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace TCP_Communication
{
class Program
{
static void Main(string[] args)
{
// ...
TCP_Communication.TcpClient.OnDataAvailable = DataArrived;
// ...
}
public void DataArrived(char[] newdata)
{
// empty for now
}
}
}
This time it gives this error message:
'TCP_Communication.TcpClient.OnDataAvailable' is a 'type', which is not valid in the given context
I read the article in MSDN about C# delegates, but still I can't find what I'm missing.
Please help me guys, how can I fix this code?
-
April 15th, 2010, 10:04 AM
#2
Re: Cannot create/invoke delegate
Your TcpClient class should be public.
Code:
public class TcpClient
{
// ...
public delegate void OnDataAvailable(char[] data);
// ...
}
-
April 16th, 2010, 05:12 AM
#3
Re: Cannot create/invoke delegate
Originally Posted by dannystommen
Your TcpClient class should be public.
Code:
public class TcpClient
{
// ...
public delegate void OnDataAvailable(char[] data);
// ...
}
Adding "public" keyword did not change anything.
-
April 17th, 2010, 09:07 AM
#4
Re: Cannot create/invoke delegate
As the error message says in the second case, the line
public delegate void OnDataAvailable(char[] data); declares a type, not a member field.
You can think of it as a class that represents a collection of methods with a specific signature - but in this case, the C# language provides a special mechanism and a special keyword ("delegate") used for such declarations.
As with nested classes, you can nest a delegate within a class, but you don't have to - you can declare it within the namespace.
For example:
Code:
namespace Test
{
// declares a TYPE VoidMethod...
public delegate void VoidMethod();
public class MyClass
{
// declares a nested TYPE MyClass.WorkerMethod...
public delegate void WorkerMethod(object obj);
}
}
But these are just type declarations, they don't really do anything.
Once you've defined the delegate types, you can have member fields of those types.
Code:
namespace Test
{
// declares a TYPE VoidMethod...
public delegate void VoidMethod();
public class MyClass
{
// declares a nested TYPE MyClass.WorkerMethod...
public delegate void WorkerMethod(object obj);
// fields
private VoidMethod voidMethodDelegate = null; // this one will, for example, be used internally, so I won't expose it
private WorkerMethod workerDelegate = null;
// A property to expose the workerDelegate field.
public WorkerMethod Worker
{
get { return workerDelegate; }
set { workerDelegate = value; }
}
}
}
To access this delegate from another class you would write:
Code:
MyClass temp = new MyClass();
temp.Worker += new SomeMethodName;
//...
// later on, invoke the delegate...
temp.Worker(someParameter);
Note that the "+=" is generally used to add methods, and "-=" is used to remove them. The invocation runs all the associated methods. You can initialize/modify the delegates in the constructor, or in some other convenient place.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|