[Sorry about the clumsy title - I couldn't figure out the best way to express what I am trying to do.]
I feel a bit silly - I just discovered a bug in the following code of mine:
private static XmlSchemaSet internalSchema = null;
private static XmlSchemaSet externalSchema = null;
private static XmlSchemaSet GetSchema(SchemaType schemaType)
{
// Lazy evaluation of schema objects - only create internal
// and external schema once
XmlSchemaSet schema =
schemaType == SchemaType.Internal ? internalSchema : externalSchema;
if (schema == null)
{
schema = new XmlSchemaSet();
schema.Add("", CreateXmlSchemaFile(schemaType));
}
return schema;
}
The intent of the code is to allocate the static members internalSchema
and externalSchema
only once, when they are first requested.
Of course there's an obvious bug with my attempted concise code - the schema
local reference is pointing to the object that internalSchema
or externalSchema
is pointing to (null
initially), but as soon as I call new
, it only reassigns the local reference, not the static member.
Is there a easy way to achieve what I am trying to do? I guess I could create another method which takes an XmlSchemaSet
via the ref
keyword, but it seems silly to create another method just for this.