C#Delegates和Events逻辑和语法问题

时间:2011-05-25 05:25:51

标签: c# events syntax delegates

正如我的代码建议的那样,我正在尝试创建一个委托,它将指向StringBuff方法BuffString,它创建一个将具有相当数量的设置的StringBuilder等。

我的问题是,出于某种原因,无论它是什么,我尝试我不能将我在Sprite类中创建的StringBuff类的引用传递给委托的构造函数而不会收到错误。在此基础上,我觉得创建一个事件可能对帮助启动委托很有用。

主要问题是我现在几乎没有理解这两个概念,以及如何将它们用作其他编程语言中允许的函数指针的替代品。

如果有人知道我需要做什么来完成这项工作,我一定会很感激。

以下是代码:

public class StringBuff
        {
            private static StringBuilder stringBuffer = new StringBuilder();

            public static StringBuilder BuffString(string _string) //--may possibly have to use IntPtr to reference stringBuffer here. 
            //This is the equivalent to the "strbuff_new" C++ method variant, designed to update the stringBuffer.
            {
                int iCounter = 0;

                stringBuffer.Append(_string + " ");

                iCounter += _string.Length + 1;

                if (iCounter == stringBuffer.Capacity - 1)
                {
                    stringBuffer.Capacity += stringBuffer.Capacity; 
                }

                return stringBuffer;
            }
        }

        public delegate void UpdateStringBuffer(StringBuff sender);

        public class Sprite : SpriteInterface.ISprite
        {
            private StringBuff stringBuff = new StringBuff();

            public event UpdateStringBuffer stringBuffEvent
            {
                add
                {
                    Console.WriteLine("Adding");
                    stringBuffEvent += value;
                }
                remove
                {
                    Console.WriteLine("Removing...");
                    stringBuffEvent -= value;
                }
            }

            static void Main()
            { 
                new Sprite().stringBuffEvent += new UpdateStringBuffer(stringBuff);
            }




        }

4 个答案:

答案 0 :(得分:2)

我相信你需要一些阅读。请参阅以下内容:

Events Tutorial

Introduction to Delegates and Events

Events and Delegates simplified

答案 1 :(得分:1)

您误解了事件的使用和委托。

如果要向事件添加事件处理程序,则传递与事件相同类型的委托(您正确执行了该事务)
但是当你创建一个委托时,你应该在构造函数中传递的内容(大多数时候)是一个方法名称而不是一些变量,因为委托是一种指向(列表)的指针功能

我建议你阅读更多关于委托的内容,因为Akram Shahda建议但是现在我会告诉你,你应该作为参数传递给委托构造函数的方法应该具有相同的签名 - 意味着返回相同的值并接受相同的参数。所以例如你可以:

// This method have the same signature as UpdateStringBufferDelegate
public void SomeMethod (StringBuff buff)  
{
    // Doing somthing here
}

然后你可以在你的主要任务:

// Passing method's name and not a variable!!
new Sprite().stringBuffEvent += new UpdateStringBuffer(SomeMethod);

将传递给函数本身的Actuall参数(某些StringBuff)仅在事件调用时确定。
你应该阅读更多相关信息。

祝你好运!

答案 2 :(得分:0)

你做错了,

 new Sprite().stringBuffEvent += new UpdateStringBuffer(stringBuff);

由于以下原因,上述代码无效。

你的UpdateStringBuffer正在使用的

1。 stringBuff是Sprite中StringBuff的一个实例。

2。您正在从静态Main方法访问stringBuff,该方法对stringBuff的位置一无所知。

答案 3 :(得分:0)

1 - 委托的构造函数只能有一个参数Method。实施例

public delegate void UpdateStringBuffer(StringBuff sender);

2 - 您可以声明ur事件并添加一个方法来定义您的Splite类中的ur方法。例如:

public event UpdateStringBuffer stringBuffEvent;

public ProcessUpdateStringBuffer(UpdateStringBuffer yourMethod)
{
       stringBuffEvent += yourMethod
}

3 - 从你的主人那里你可以定义事件的ur方法并像这样调用它:

Sprite sprite = new Sprite();
sprite.ProcessUpdateStringBuffer(UpdateStringBuffer(urMethod));
sprite.stringBuffEvent(ur parameters);
相关问题