N
None
Hi,
I'm facing a problem with static instances. I have created a
class called CustomList by deriving the class List<int>. Inside the
CustomList i have created Remove event (when any item is removed from
the List). After that i have created a staic instance for the
CustomList in my WebService class. The class contains the Start And
Poll Methods. Inside the Start method i have added the Removed event.
Please execute the following code and you will notice that the
"objCustList_Removed" is called more than one time. I'm in a situation
that the "objCustList_Removed" function should not called more than
one time. Please let me know if anybody find solution.
Note : Also i'm in a situation that i want to use only static
instance.
using System;
using System.Collections.Generic;
using System.Text;
namespace TestConsole
{
class Main
{
public delegate void OnRemovedEventHandler(object sender,
System.EventArgs e);
public class CustomList : List<int>
{
public event OnRemovedEventHandler Removed;
protected virtual void OnRemoved(System.EventArgs e)
{
if (Removed != null)
{
Removed(this, e);
}
}
public new void Add(int i)
{
base.Add(i);
}
public new void Remove(int i)
{
base.Remove(i);
Removed(this, System.EventArgs.Empty);
}
public new void RemoveAt(int i)
{
base.RemoveAt(i);
Removed(this, System.EventArgs.Empty);
}
}
public class WebService
{
private static CustomList objCustList = new CustomList();
public WebService()
{
}
public int start(int i)
{
objCustList.Removed += new
OnRemovedEventHandler(objCustList_Removed);
objCustList.Add(i);
return i;
}
void objCustList_Removed(object sender, EventArgs e)
{
Console.WriteLine("Method Called More than one time");
}
public void poll(int i)
{
objCustList.Remove(i);
}
}
internal class Program
{
public static void Main()
{
WebService ws = new WebService();
int i1 = ws.start(1);
WebService ws1 = new WebService();
int i2 = ws1.start(2);
WebService ws2 = new WebService();
int i3 = ws2.start(3);
WebService ws3 = new WebService();
int i4 = ws3.start(4);
WebService ws4 = new WebService();
int i5 = ws4.start(5);
ws1.poll(i1);
Console.WriteLine("---------------------------");
ws2.poll(i2);
Console.WriteLine("---------------------------");
ws3.poll(i3);
Console.WriteLine("---------------------------");
ws4.poll(i4);
Console.WriteLine("---------------------------");
ws4.poll(i5);
Console.WriteLine("---------------------------");
Console.ReadLine();
}
}
}
}
Thanks,
Vinothkumar B
I'm facing a problem with static instances. I have created a
class called CustomList by deriving the class List<int>. Inside the
CustomList i have created Remove event (when any item is removed from
the List). After that i have created a staic instance for the
CustomList in my WebService class. The class contains the Start And
Poll Methods. Inside the Start method i have added the Removed event.
Please execute the following code and you will notice that the
"objCustList_Removed" is called more than one time. I'm in a situation
that the "objCustList_Removed" function should not called more than
one time. Please let me know if anybody find solution.
Note : Also i'm in a situation that i want to use only static
instance.
using System;
using System.Collections.Generic;
using System.Text;
namespace TestConsole
{
class Main
{
public delegate void OnRemovedEventHandler(object sender,
System.EventArgs e);
public class CustomList : List<int>
{
public event OnRemovedEventHandler Removed;
protected virtual void OnRemoved(System.EventArgs e)
{
if (Removed != null)
{
Removed(this, e);
}
}
public new void Add(int i)
{
base.Add(i);
}
public new void Remove(int i)
{
base.Remove(i);
Removed(this, System.EventArgs.Empty);
}
public new void RemoveAt(int i)
{
base.RemoveAt(i);
Removed(this, System.EventArgs.Empty);
}
}
public class WebService
{
private static CustomList objCustList = new CustomList();
public WebService()
{
}
public int start(int i)
{
objCustList.Removed += new
OnRemovedEventHandler(objCustList_Removed);
objCustList.Add(i);
return i;
}
void objCustList_Removed(object sender, EventArgs e)
{
Console.WriteLine("Method Called More than one time");
}
public void poll(int i)
{
objCustList.Remove(i);
}
}
internal class Program
{
public static void Main()
{
WebService ws = new WebService();
int i1 = ws.start(1);
WebService ws1 = new WebService();
int i2 = ws1.start(2);
WebService ws2 = new WebService();
int i3 = ws2.start(3);
WebService ws3 = new WebService();
int i4 = ws3.start(4);
WebService ws4 = new WebService();
int i5 = ws4.start(5);
ws1.poll(i1);
Console.WriteLine("---------------------------");
ws2.poll(i2);
Console.WriteLine("---------------------------");
ws3.poll(i3);
Console.WriteLine("---------------------------");
ws4.poll(i4);
Console.WriteLine("---------------------------");
ws4.poll(i5);
Console.WriteLine("---------------------------");
Console.ReadLine();
}
}
}
}
Thanks,
Vinothkumar B