Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to Firing event when instance created? in C#
Posted
Comments
Kishor Deshpande 23-Jan-13 12:08pm    
You want to raise event on which instance creation?
Please share atleast some description.
Mir Hassan Moosavi 24-Jan-13 4:23am    
my problem image is here:

http://www.freeimagehosting.net/1msij
joshrduncan2012 23-Jan-13 12:08pm    
What IDE? What is the context of how you are wanting to do this? Please improve question and add more info as to what you are referring to.
Mir Hassan Moosavi 24-Jan-13 4:24am    
my problem image is here:

http://www.freeimagehosting.net/1msij
Sergey Alexandrovich Kryukov 23-Jan-13 12:30pm    
Instance of what?!
—SA

What an odd request! You can't do it with "normal" events, because you can't hook a handler to them until the instance constructor is finished (there is no instance to hook the handler to until then!)
You can do it, but it needs to be a Static Event:
C#
public class XX
    {
    public static event EventHandler Created;
    public string Text { get; set; }
    public XX(string n)
        {
        Text = n;
        if (Created != null)
            {
            Created(this, null);
            }
        }
    }
You can then hook to it and so forth:
C#
XX.Created += new EventHandler(XX_Created);
...
XX x = new XX("ONE");
XX y = new XX("Two");
It's a strange request though.
 
Share this answer
 
Comments
BC @ CV 23-Jan-13 15:54pm    
Why is this voted a 1? This is good code that answers the question. +5
Jibesh 23-Jan-13 17:53pm    
Nice answer +5
Mir Hassan Moosavi 24-Jan-13 2:44am    
This is not work..What's XX_Created ?!
OriginalGriff 24-Jan-13 3:33am    
An event handler method name.
Since I don't know what you want to do, there is not a lot of point in my creating a handler method...
Mir Hassan Moosavi 24-Jan-13 3:01am    
XX.Created += new EventHandler(XX_Created);

Where Set this?!?!? - In Constructor Or other Places ?
namespace byMirHassanMoosavi /*This ProblemSet is very Easy!!*/
{
#region UsingScope
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion

#region ProblemSet
namespace evtFiring
{
#region SolutionScope
public delegate void OnCreateEvent(); //delegate declaration

[Serializable()]
public class Person
{
public static event OnCreateEvent CreateEvent; //static event declaration (delegate in use!)

#region Props & Field
public string Name { get; set; }
public string Family { get; set; }
public string SerializableStatus = string.Empty;
#endregion

#region Constructor
public Person() //default Constructor & event firing place
{
this.Name = "unknown-name!";
this.Family = "unknown-family!";
//Using Lambda Expression for declare an anonymous method
Person.CreateEvent += new OnCreateEvent(() =>
{
#region ChangeConsoleTheme
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
#endregion

if (this.GetType().IsSerializable == true)
this.SerializableStatus = "is";
else SerializableStatus = "is not";

Console.WriteLine(
"\r\nThe Instance of Person Class Created in - {0} - with \r\nFollowing Information|>\r\n\r\n\t|> Name: {1}\t\r\n\t|> Family: {2}\t\r\n\r\n\t|> {1} {2} HashCode Is: {3}\t\r\n\t|> {1} {2} Type Is: {4}\t\r\n\t\r\n\t|> {1} {2} {5} Serializable.\r\n",
DateTime.Now.ToString(),
this.Name, this.Family,
this.GetHashCode().ToString(), this.GetType().Name, SerializableStatus);
});
//First Check then fire!!
if (CreateEvent != null)
{
CreateEvent();
}
}
public Person(string name, string family)//Main Constructor
{
this.Name = name;
this.Family = family;
//Using Lambda Expression for declare an anonymous method
Person.CreateEvent += new OnCreateEvent(() =>
{
#region ChangeConsoleTheme
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
#endregion

if (this.GetType().IsSerializable == true)
this.SerializableStatus = "is";
else SerializableStatus = "is not";

Console.WriteLine(
"\r\nThe Instance of Person Class Created in - {0} - with \r\nFollowing Information|>\r\n\r\n\t|> Name: {1}\t\r\n\t|> Family: {2}\t\r\n\r\n\t|> {1} {2} HashCode Is: {3}\t\r\n\t|> {1} {2} Type Is: {4}\t\r\n\t\r\n\t|> {1} {2} {5} Serializable.\r\n",
DateTime.Now.ToString(),
this.Name, this.Family,
this.GetHashCode().ToString(), this.GetType().Name, SerializableStatus);
});
//First Check then fire!!
if (CreateEvent != null)
{
CreateEvent();
}
}
#endregion
}
#endregion

#region Using'Solution'
class Program
{
static void Main(string[] args)
{
Person p = new Person("MirHassan", "Moosavi");

Console.WriteLine("------------------------------------------------------------------------");

//Person p2 = new Person();



Console.ReadKey();

}
}
#endregion
}
#endregion
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900