Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Cannot create/invoke delegate (http://www.go4expert.com/forums/create-invoke-delegate-t21783/)

AhmedHan 16Apr2010 15:33

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:
Quote:

'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:
Quote:

'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?

AhmedHan 16Apr2010 15:57

Re: Cannot create/invoke delegate
 
I tried writing "public class {};" instead of "class {}", but it didn't do any good.


All times are GMT +5.5. The time now is 13:36.