Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Delegate question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 31 Mar 2008 06:36 GMT
Here is a stripped down version of a delegate I am trying to get to work,
which is how an example program shows:

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

namespace delegate4
{
   class Program
   {
       delegate void Print(string s);

       static void Main(string[] args)
       {
           // not anonymous - must have a method already set up to call
           Print delegateVariable = new Print(realMethod);
<--------
           delegateVariable("This is our non-anonymous delegate string");
       }
       public void realMethod(string myString)
       {
           Console.WriteLine(myString);
       }
   }
}

This gives me an error:

An object reference is required for the nonstatic field, method, or property
'delegate4.Program.realMethod(string)'

Why doesn't this work?

Thanks,

Tom
Alberto Poblacion - 31 Mar 2008 07:18 GMT
> Here is a stripped down version of a delegate I am trying to get to work,
> which is how an example program shows:
[quoted text clipped - 28 lines]
>
> Why doesn't this work?

    It doesn't work because you are calling an instance method
("realMethod") from a static method ("Main"). The fix is to either declare
"realMethod" as static, or create an instance of class "Program" and
initialize your delegate to point to theInstance.realMethod.
Jon Skeet [C# MVP] - 31 Mar 2008 08:55 GMT
> Here is a stripped down version of a delegate I am trying to get to work,
> which is how an example program shows:
[quoted text clipped - 29 lines]
>
> Why doesn't this work?

realMethod is an instance method - it needs to know which instance of
Program it's being called on (the *target*).

Your options are:

1) Make the method static
2) Create an instance of Program and use that to create the delegate,
e.g.

Program prog = new Program();
Print delegateVariable = prog.RealMethod;

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

tshad - 31 Mar 2008 16:37 GMT
>> Here is a stripped down version of a delegate I am trying to get to
>> work, which is how an example program shows:
[quoted text clipped - 41 lines]
> Program prog = new Program();
> Print delegateVariable = prog.RealMethod;

Makes sense.

Thanks,

Tom

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.