Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
How to Firing event when instance created? in C#
Posted 23-Jan-13 6:03am
Comments
Kishor Deshpande at 23-Jan-13 12:08pm
   
You want to raise event on which instance creation?
Please share atleast some description.
Mir Hassan Shams Moosavi at 24-Jan-13 4:23am
   
my problem image is here:
 
http://www.freeimagehosting.net/1msij
joshrduncan2012 at 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 Shams Moosavi at 24-Jan-13 4:24am
   
my problem image is here:
 
http://www.freeimagehosting.net/1msij
Sergey Alexandrovich Kryukov at 23-Jan-13 12:30pm
   
Instance of what?!
—SA
jibesh at 23-Jan-13 17:52pm
   
Probably an interview question ;)
Mir Hassan Shams Moosavi at 24-Jan-13 4:24am
   
my problem image is here:
 
http://www.freeimagehosting.net/1msij
Sergey Alexandrovich Kryukov at 24-Jan-13 12:39pm
   
What an odd request!
—SA
Mir Hassan Shams Moosavi at 24-Jan-13 2:41am
   
I firing event when instance of my class Created in C#,(in VS10)-
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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:
XX.Created += new EventHandler(XX_Created);
...
XX x = new XX("ONE");
XX y = new XX("Two");
It's a strange request though.
  Permalink  
Comments
BC @ CV at 23-Jan-13 15:54pm
   
Why is this voted a 1? This is good code that answers the question. +5
jibesh at 23-Jan-13 17:53pm
   
Nice answer +5
Mir Hassan Shams Moosavi at 24-Jan-13 2:44am
   
This is not work..What's XX_Created ?!
OriginalGriff at 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 Shams Moosavi at 24-Jan-13 3:01am
   
XX.Created += new EventHandler(XX_Created);
 
Where Set this?!?!? - In Constructor Or other Places ?
OriginalGriff at 24-Jan-13 3:32am
   
Up to you - probably, you need it fairly early in your program if you are to detect instance creation: it depends on what you are trying to monitor.
Mir Hassan Shams Moosavi at 24-Jan-13 3:48am
   
I want to when my Instance Created Event firing and for example changing Inner Property value and Say me ..hey "I am your Instance!. My Property Changed"
Mir Hassan Shams Moosavi at 24-Jan-13 3:55am
   
Realy tanX..But ForExample.I want:
 
static void Main(string[] args){
 
XX x = new XX(); // This is Only Code Line in Main Method
}
==> in Console Output write ==> x Instance Created (x From XX Class) .
OriginalGriff at 24-Jan-13 4:26am
   
Then you have to add the event handler as the first instruction in Main.
Until you have added the handler, nothing will happen (as the event does not have anything to call)
You can't get the name of the variable it is being assigned into, as (for one thing) that hasn't happened until after the constructor is complete.
Mir Hassan Shams Moosavi at 24-Jan-13 4:22am
   
my problem image is here:
 
http://www.freeimagehosting.net/1msij
Mir Hassan Shams Moosavi at 24-Jan-13 3:12am
   
How to implement this in Real..i', confused :(
Mir Hassan Shams Moosavi at 24-Jan-13 4:21am
   
my problem image is here:
 
http://www.freeimagehosting.net/1msij
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 365
1 CPallini 335
2 BillWoodruff 324
3 George Jonsson 319
4 Gihan Liyanage 225
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100