Groups | Blog | Home
all groups > dotnet general > april 2007 >

dotnet general : So simple question for C# user ??


calderara
4/30/2007 1:34:00 PM
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
calderara
4/30/2007 2:00:03 PM
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

[quoted text, click to view]
calderara
4/30/2007 2:02:00 PM
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

[quoted text, click to view]
Page Brooks
4/30/2007 4:43:30 PM
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();

}

}

....



--
Page Brooks
www.explosivedog.com


[quoted text, click to view]

Jon Skeet [C# MVP]
4/30/2007 11:02:17 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button