Cannot create/invoke delegate
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Cannot create/invoke delegate

  1. #1
    Join Date
    Dec 2009
    Posts
    11

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

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Cannot create/invoke delegate

    Your TcpClient class should be public.

    Code:
    	public class TcpClient
    	{
    		// ...
    		public delegate void OnDataAvailable(char[] data);
    		// ...
    	}

  3. #3
    Join Date
    Dec 2009
    Posts
    11

    Re: Cannot create/invoke delegate

    Quote Originally Posted by dannystommen View Post
    Your TcpClient class should be public.

    Code:
    	public class TcpClient
    	{
    		// ...
    		public delegate void OnDataAvailable(char[] data);
    		// ...
    	}
    Adding "public" keyword did not change anything.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,099

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center