- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 5882
Code to write this article I have stolen from here.
Code looks like this:
class Program { static void Main(string[] args) { MyEvents me = new MyEvents(); Listener l = new Listener(); l.Subscribe(me); me.Start(); } } public class MyEvents { public event MyEventHandler MyEvent; public EventArgs e = null; public delegate void MyEventHandler(object sender, EventArgs e); public void Start() { while (true) { System.Threading.Thread.Sleep(3000); if (MyEvent != null) { MyEvent(this, e); } } } } public class Listener { public EventArgs e = null; public void Subscribe(MyEvents m) { m.MyEvent += new MyEvents.MyEventHandler(HeardIt); } private void HeardIt(object sender, EventArgs e) { System.Console.WriteLine("Test"); } }
Now, lets try to analyze it part by part.
If we start from beginning, you will see code like:
static void Main(string[] args) { MyEvents me = new MyEvents(); Listener l = new Listener(); l.Subscribe(me); me.Start(); }
Then in events class notice that we had to define delegate like:
public delegate void MyEventHandler(object sender, EventArgs e);
Our event will then use delegate like:
public event MyEventHandler MyEvent;
And we called our event (delegate) method like:
MyEvent(this, e);
In this moment application still has no idea about MyEvents implementation.
We have to define "Listener" which will listen our event, and with that, we will actually implement our delegate.
First we have to "subscribe" to an event, like:
m.MyEvent += new MyEvents.MyEventHandler(HeardIt);
And finally implementation:
private void HeardIt(object sender, EventArgs e) { System.Console.WriteLine("Test"); }
Example project you can download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 5491
Just as a note to my self, here I showed one way of getting exe path, another way can be something like this:
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Afile.xml")
So, with line:
Assembly.GetExecutingAssembly().Location
we are receiving full path, then with line:
Path.GetDirectoryName(...)
we are getting just a folder name, and with line:
Path.Combine(...)
we are combining path with our file name.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 4904
I was playing little bit with classes. Idea is to see how it will behave if we declare method with type as current class instance to which this method belong.
So, I have declared class like:
public class MyClass { public string mOne = "none"; public string mTwo = "none"; public string mAll = "none"; public MyClass MyMethodOne() { this.mOne = "I am from method one"; this.mAll = "My name is all and I am also from method one"; return this; } public MyClass MyMethodTwo() { this.mTwo = "I am from method two"; this.mAll = "My name is all and I am also from method two"; return this; } public MyClass MyMethodNewInstanceOne() { MyClass something = new MyClass(); something.mOne = "I am from method one"; something.mAll = "My name is all and I am also from method one"; return this; } public MyClass MyMethodNewInstanceTwo() { MyClass something = new MyClass(); something.mTwo = "I am from method two"; something.mAll = "My name is all and I am also from method two"; return something; } }
Notice for example:
this.mTwo = "I am from method two";
return this;
Here we are not creating new instance, so if we do call like:
MyClass myClassesResult = playingWithClasses.MyMethodOne().MyMethodTwo();
We will not loose any changes
That means that if we create console application like:
MyClass playingWithClasses = new MyClass(); MyClass myClassesResult = playingWithClasses.MyMethodOne().MyMethodTwo(); Console.WriteLine("************* without new instance *************"); Console.WriteLine("One: " + myClassesResult.mOne); Console.WriteLine("Two: " + myClassesResult.mTwo); Console.WriteLine("All: " + myClassesResult.mAll);
Result will be like:
************* without new instance *************
One: I am from method one
Two: I am from method two
All: My name is all and I am also from method two
Then notice for example:
MyClass something = new MyClass();
something.mTwo = "I am from method two";
return something;
That means that if we create console application like:
MyClass playingWithClasses = new MyClass(); MyClass myClassesNewInstanceResult = playingWithClasses.MyMethodNewInstanceOne().MyMethodNewInstanceTwo(); Console.WriteLine("************* with new instance *************"); Console.WriteLine("One: " + myClassesNewInstanceResult.mOne); Console.WriteLine("Two: " + myClassesNewInstanceResult.mTwo); Console.WriteLine("All: " + myClassesNewInstanceResult.mAll);
Result will be like:
************* with new instance *************
One: none
Two: I am from method two
All: My name is all and I am also from method two
Example project you can download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 6840
Here I explained how changing one XML in class library can lead to hell. It seems that another solution would be to add XML as a link.
To add XML as a link right click on the project, click add Existing item:
Choose XML and choose add as link from drop down menu:
And that should be it...