Your second post shows it set to a parameter of type FObject (pOFSystem) but doesn't show the actual declaration for the field.

Is that is the case, I can't explain the error since FObject does indeed appear to have a public constructor that accepts no arguments.

I copied your code into a blank project and commented out any lines that referenced objects/classes not in the post, and it compiled and ran fine (VS2008). Is it possible some part of your solution needs a rebuild ?

If it refers to the field in line 1, then your code won't compile because the field has not yet been initialized.

On the other hand, called using the typeof operator, the code compiles and runs happily.

FSystem = (FObject)Activator.CreateInstance(typeof(FObject), false);

EDIT: Just notice the additional code where you defined OFSystem. If OFSystem was initialized correctly you code should work since type FObject, from what I can see, does have a parameterless constructor.

The CreateInstance method uses Reflection to instantiate the object. When you do not provide one the compiler will provide one for you in IL. The JIT compiler does not have this luxury, it requires a default parameterless constructor.

Just add one, 3 lines of code. That should fix it. This can come up when you de-serialize objects, too.

Rudedog =8^DMark the best replies as answers. "Fooling computers since 1971."

The CreateInstance method uses Reflection to instantiate the object. When you do not provide one the compiler will provide one for you in IL. The JIT compiler does not have this luxury, it requires a default parameterless constructor.

Just add one, 3 lines of code. That should fix it. This can come up when you de-serialize objects, too.

Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."

I considered that, and was going to (but didn't suggest) adding a constructor to it. The reason I didn't suggest it is because his code works fine on my PC even without adding a public constructor to listener, and the code I compiled and ran has the same inheritance chain.

I guess it's worth a shot though.

Harry,

Try adding

public Listener(){}

to the Listener class as Rudedog suggests.

Rudedog, can you explain why two of us can run the code succesfully without adding a parameterless constructor to the Listener class ?

One explanation is that your TListener class has no constructors defined at all, but the OP's class has constructor that have constructors that require parameters but no parameterless constructor.Mark the best replies as answers. "Fooling computers since 1971."

That's why I suggested he rebuild his solution, but he claims a 'clean build' didn't fix it.I guess he could check the dates and times on all the assemblies in the folder he's running from and make sure they're all 'recent'.

The OP can browse the types in the Object Browser Window, set it for the current solution. This will show the types defined for the currently loaded assemblies as Hans suggested. Every type in the inheritance chain needs a public parameterless constructor all the way to System.Object.

I have added the changes you have suggested and this still isnt working. I can assure you that I have completed a clean build and that my assemblies are recent. Just for clarity I am adding the code again with the changes I have made.

I trust what the compiler tells me.It is far more likely that I have made a mistake than the compiler.

Adding a default constructor to all classes is a best practice.

If Listener didn't have a parameterless constructor, the compiler would complain because of the call from the constructor of FObject to the constructor in its base class. Seems to me the OP is talking about a runtime error.