C# 委托和事件逻辑和语法问题
正如我的代码所示,我正在尝试创建一个委托,该委托将指向 StringBuff 方法 BuffString,该委托将创建一个 StringBuilder,该 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);
}
}
As my code suggests, I'm trying to create a delegate which will point to the StringBuff method BuffString, which creates a StringBuilder that is going to have a fair amount of settings, etc.
My problem is that, for some reason, no matter what it is I try I can't pass the reference to the StringBuff class I made within my Sprite class to the delegate's constructor without receiving an error. Ontop of that, I feel like creating an event may be useful to help initiate the delegate.
The main problem is that I'm just now barely grasping these two concepts, as well as how to use them as replacements for function pointers which are allowed in other programming languages.
If anyone has any idea on what it is I need to do to make this work, I would definitely appreciate it.
Here's the code:
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信你需要一些阅读。请参阅以下内容:
事件教程
代表和活动简介
简化的事件和代表
I believe you are in need for some reading. Refer to the following:
Events Tutorial
Introduction to Delegates and Events
Events and Delegates simplified
您误解了事件和委托的使用。
当您想要向事件添加事件处理程序时,您可以传递与事件相同类型的委托(您做得正确)
但是,当您创建委托时,您应该在构造函数中传递的内容(大多数情况下)是方法名称,而不是某个变量,因为委托是一种指向(列表)的指针功能。
我建议您阅读 Akram Shahda 建议的有关委托的更多信息,但现在我会告诉您,应作为参数传递给委托构造函数的方法应该具有相同的签名 - 意味着返回相同的值并接受相同的参数。因此,例如您可以:
然后您可以在 main 中执行以下操作:
将传递给函数本身的实际参数(某些 StringBuff)仅在事件调用时确定。
您应该阅读更多相关内容。
祝你好运!
You are misunderstanding the use of events and delegate.
When you want to add an Event Handler to an event, you pass a delegate of the same type as the event (which you did correctly)
But when you create a delegate, what you should pass in the constructor (most of the time) is a Method Name and not some variable, since a delegate is a kind of pointer to a (list of) functions.
I reccomend you to read more about delegates as Akram Shahda suggested but just for now i'll tell you that the method that you should pass as parameter to the delegate constructor should have the same signature - means return the same value and accept the same parameters. so for example you could have:
And then you can do in your main:
The Actuall parameters that will be passed to the function itself (some StringBuff) only determined at the time of the invokation of the event.
You should read more about that.
Good Luck!
您做错了,
由于以下原因,上述代码无效。
1. UpdateStringBuffer 所采用的 stringBuff 是 Sprite 中 StringBuff 的一个实例。
2. 您正在从静态 Main 方法访问 stringBuff,该方法不知道 stringBuff 所在的位置。
you are doing it wrong,
Above code is invalid due to following reasons.
1. stringBuff that your UpdateStringBuffer is taking is an instance of StringBuff within Sprite.
2. You are accessing stringBuff from the static Main method which does not have any idea about stringBuff where it is located.
1 - 委托的构造函数只能有一个参数方法。 Ex
2- 您可以声明您的事件并添加一个方法来在您的 Splite 类中定义您的方法。例如:
3- 从您的 main 中,您可以定义事件的方法并像这样调用它:
1- The delegate's constructor can only have a parameter Method. Ex
2- You can declare ur event and add a method to define ur method in ur Splite class. Ex:
3- and from ur main u can define ur method to the event and invoke it like this: