I have a class called ClassModel
. This is how it looks.
class ClassModel
{
dynamic ConnListInstance;
public ClassModel() {
ConnListInstance = Activator.CreateInstance(Type.GetTypeFromProgID("PCOMM.autECLConnlist"));
}
public void checkCount() { //this shows a count of 0
Console.WriteLine(ConnListInstance.Count());
}
public void checkCountVersionTwo() { //this shows a count of 1
ConnListInstance = Activator.CreateInstance(Type.GetTypeFromProgID("PCOMM.autECLConnlist"));
Console.WriteLine(ConnListInstance.Count());
}
}
I have instantiated the class in my main page by declaring ClassModel obj = new ClassModel()
.
But when I try calling the checkCount
method, it returns 0 instead of 1. The checkCountVersionTwo
returns 1 but only because I have added the instantiation from the constructor.
Is there something wrong with the way I have created my constructor and class? May I know why it is returning a null/empty value? Shouldn't the variable ConnListInstance
have a value upon creating a new ClassModel
object?
This has nothing to do with your code, but the reason is in the way how this object works.
Please read the documentation:
(emphasis mine)
So the solution is: