G
Guest
I have three classes. One (Class ObjectC) and two other classes (Class1 and
Class2) that will hold instances of the ObjectC class. I would like to
transfer an instance of OjectC from Class1 into Class2, while making sure
that no resource leak occurs. Is the following the best way to hanlde this?
Is there a more efficient way?
public class ObjectC
{
public ObjectC()
{
// Initialize some data.
}
public void Dispose()
{
// Destroy initialized data.
}
}
public class Class1 : System.Collections.CollectionBase
{
public Class1()
{
}
public void CreatObject()
{
ObjectC oc = new ObjectC();
List.Add(oc);
}
public void TransferObject(Class2 c2)
{
if(List.Count>0)
{
c2.AddObject(List[0]);
((ObjectC)List[0]).Dispose();
List.RemoveAt(0);
}
}
}
public class Class2 : System.Collections.CollectionBase
{
public Class2()
{
}
public void AddObject(Object o)
{
List.Add(o);
}
}
At execution time I would then:
Class1 theC1 = new Class1();
Class2 theC2 = new Class2();
// Create 100 ObjectC objects.
for(int i=0;i<100;i++)
{
theC1.CreateObject();
}
I then would transfer the newly instantiated ObjectC objects into theC2 from
theC1:
// Transfer 100 ObjectC objects from theC1 to theC2.
for(int i=0;i<100;i++)
{
theC1.TransferObject(theC2);
}
Any ideas or comments welcomed.
Class2) that will hold instances of the ObjectC class. I would like to
transfer an instance of OjectC from Class1 into Class2, while making sure
that no resource leak occurs. Is the following the best way to hanlde this?
Is there a more efficient way?
public class ObjectC
{
public ObjectC()
{
// Initialize some data.
}
public void Dispose()
{
// Destroy initialized data.
}
}
public class Class1 : System.Collections.CollectionBase
{
public Class1()
{
}
public void CreatObject()
{
ObjectC oc = new ObjectC();
List.Add(oc);
}
public void TransferObject(Class2 c2)
{
if(List.Count>0)
{
c2.AddObject(List[0]);
((ObjectC)List[0]).Dispose();
List.RemoveAt(0);
}
}
}
public class Class2 : System.Collections.CollectionBase
{
public Class2()
{
}
public void AddObject(Object o)
{
List.Add(o);
}
}
At execution time I would then:
Class1 theC1 = new Class1();
Class2 theC2 = new Class2();
// Create 100 ObjectC objects.
for(int i=0;i<100;i++)
{
theC1.CreateObject();
}
I then would transfer the newly instantiated ObjectC objects into theC2 from
theC1:
// Transfer 100 ObjectC objects from theC1 to theC2.
for(int i=0;i<100;i++)
{
theC1.TransferObject(theC2);
}
Any ideas or comments welcomed.