الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خطأ في كود Delegate

بدأه Discover_2010 في 27 يناير 2009 · 6 رد · 922 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

اخواني انا لسه بتعلم Delegate وعندما بدأت بكتابة الكود واردت انفذه مارضى يتنفذ هذا الكود ..فلماذا ؟!

class test
	{
		delegate void linker();
		private linker linker1;

		public test()
		{
			this.linker1 += test.show;

		}

	   static void show()
		{
			Console.WriteLine("I am Happy");

		}


	}

	class Program
	{
		static void Main(string[] args)
		{
			test rar = new test();
		}
	}

تم تعديل هذه المشاركة بواسطة Discover_2010 في 27 يناير 2009 في 03:01

#2

السلام عليكــم ورحمـة الله وبركاتــه

الكود أموره عال العال و المشكل هو أن قمت فقط بالتقديم للقسم دون إستدعاء دواله.

المشكلة التي قد تعترضها تتمثل في إستخدام الدالة show

فهي من نوعstatic و إستخدامها يحد من الحرية

#3

اخي انا اللي فهمتمه من كلامك اني أجعل show عامه أي public ولكن الكود لم يعمل

#4

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class test
{
delegate void linker();
private linker linker1;

public test()
{
this.linker1 += test.show;
linker1.Invoke();

}

static void show()
{
Console.WriteLine("I AM Happy");

}

}

class Program
{
static void Main(string[] args)
{
test rar = new test();
Console.ReadLine();
}
}
}
using System;

i think this modification of your code make you happy, any you've created a delegate in the proper way and make it pointing to the show method but when you will invoke this delegate to get the result of the method in which it point to, anyway for further details your free to comment

Basslk

http://blogs.windowsclient.net/basslk

.NET Application Developer

MCSE

#6

before answer your question about the invoke part, i encourage you to read more about delegates before try to use them, MSDN is full with articles about this. Any way invoke method is calling the method that your delegate is pointing to

Basslk

http://blogs.windowsclient.net/basslk

.NET Application Developer

MCSE

مواضيع مشابهة