So simple question for C# user ??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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();

}

}

....
 
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

Page Brooks said:
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


calderara said:
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
 
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

Page Brooks said:
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


calderara said:
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
 
calderara said:
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.
 
Back
Top