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 / .NET Framework / General / April 2007

Tip: Looking for answers? Try searching our database.

So simple question for C# user ??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
calderara - 30 Apr 2007 21:34 GMT
Dear all,

Until now I was programing for more that 6 years using VB.net simply because
I come from that word and used to. Now I need to switch to C# simply because
my customer want its project in that language. So now I am fighting with so
simple thing like raising a simple event as follow, and have a complie error,
with no damn idea what this compiler want.

The code is as follow

------>

namespace ConsoleApplication1
{
   public delegate void dg_Startup();

   class Program
   {
       
       public event dg_Startup StartupComplete;
       
       static void Main(string[] args)
       {
        Console.WriteLine("Initiliastion");
        Console.Write("completetd...");
        StartupComplete;
        Console.ReadLine;
       }
           
   }
 
}
<-------

The two last line of my main are highligtied with error saying :

Error    1    Only assignment, call, increment, decrement, and new object
expressions can be used as a statement    D:\Users\Serge\Documents\Visual Studio
2005\Projects\WindowsService1\ConsoleApplication1\Program.cs    18    10    ConsoleApplication1

Error    2    An object reference is required for the nonstatic field, method, or
property
'ConsoleApplication1.Program.StartupComplete'    D:\Users\Serge\Documents\Visual
Studio
2005\Projects\WindowsService1\ConsoleApplication1\Program.cs    18    10    ConsoleApplication1

Thnaksa for help
serge
Page Brooks - 30 Apr 2007 21:43 GMT
Calderara,
Try something like this:

class Program
{

static void Main(string[] args)

{

MyClass mc = new MyClass();

mc.StartupComplete += new MyClass.dg_Startup(mc_StartupComplete);

mc.Start();

}

static void mc_StartupComplete()

{

Console.WriteLine("Startup Complete");

}

}

...
...
...

public class MyClass
{

public delegate void dg_Startup();

public event dg_Startup StartupComplete;

public void Start()

{

  if (StartupComplete != null)

     StartupComplete();

}

}

...

Signature

Page Brooks
www.explosivedog.com

> Dear all,
>
[quoted text clipped - 53 lines]
> Thnaksa for help
> serge
calderara - 30 Apr 2007 22:00 GMT
hi thanks for your reply...
Could you explain a bit...

What i understand here is that you need to create a new class to raise an
event that you have decared on an other one ?

I am just writeing a test console application on witch I have delcare a
delegate for that event then during the main function, which is in a way the
initliasation oart I just need to raise it at that time !!
Juts for this we need to creat an extra class ?

regards
serge

> Calderara,
> Try something like this:
[quoted text clipped - 106 lines]
> > Thnaksa for help
> > serge
calderara - 30 Apr 2007 22:02 GMT
IN fact I simply want to creat a class which raise an event.
Then from an other assembly I will register to that event and catch it.

So in my test, the smal console app should only raise the event for others
which are register to it..

???
serge

> Calderara,
> Try something like this:
[quoted text clipped - 106 lines]
> > Thnaksa for help
> > serge
Jon Skeet [C# MVP] - 30 Apr 2007 23:02 GMT
> Until now I was programing for more that 6 years using VB.net simply because
> I come from that word and used to. Now I need to switch to C# simply because
> my customer want its project in that language. So now I am fighting with so
> simple thing like raising a simple event as follow, and have a complie error,
> with no damn idea what this compiler want.

Brackets, that's all, and a using statement at the start -

using System;

...
StartupComplete();
Console.ReadLine();

After that, you've just got two more issues:
1) You're accessing StartupComplete from within a static method, but
it's an *instance* event (and therefore an instance field has been
created)

2) StartupComplete(); will throw a NullReferenceException if nothing
has subscribed to it.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


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.