Collected Wisdom of my latest Coding Sessions: Various Design Patterns

23 Nov 2010
Posted by xeophin

Iterating over a Dictionary in C#

Iterating over a dictionary in order to change its values will not work; the program will throw an exception because the dictionary has been modified while iterating over it (which was, frankly, the purpose, but I digress). I can see that this could be problematic. Workaround: copy the dictionary into another temporary one, iterate over that to change the original dictionary. Or create a new dictionary using the iteration.

Allowing Various Elements in no Particular Order in Relax NG

In order to allow several element within another element, but without enforcing a strict order of those elements, use a combination of <zeroOrMore> and <choice>, like this:

<element name="foo">
    <zeroOrMore>
        <choice>
            <element name="bar" />
            <element name="foobar" />
            <element name="blubb">
                <attribute name="blabb">
                    <text />
                </attribute>
                <text />
            </element>
        </choice>
    </zeroOrMore>
</element>

Serialising Objects that Derive from MonoBehaviour

Sadly enough, serialising objects that derive from MonoBehaviour, the standard class in Unity 3D, is not possible.

Instead, you will have to use the Memento pattern: create a second class (the memento) that will hold all the necessary information, and when you want to serialise it, you create a new memento instance, copy all the information over and serialise the memento instance.

Upon deserialising, you do it the opposite way. A tutorial can be found over at O'Reilly.net

It is not exactly as simple as I hoped serialising would be, but I guess it still beats actually writing all by myself the necessary code to write and read from an XML file from scratch.

## Iterating over a `Dictionary` in C# ##Iterating over a `dictionary` in order to change its values will not work; the program will throw an exception because the dictionary has been modified while iterating over it (which was, frankly, the purpose, but I digress). I can see that this could be problematic. Workaround: copy the dictionary into another temporary one, iterate over that to change the original dictionary. Or create a new dictionary using the iteration.## Allowing Various Elements in no Particular Order in Relax NG ##In order to allow several element within another element, but without enforcing a strict order of those elements, use a combination of `` and ``, like this: ## Serialising Objects that Derive from MonoBehaviour ##Sadly enough, serialising objects that derive from `MonoBehaviour`, the standard class in Unity 3D, is not possible.Instead, you will have to use the *Memento pattern*: create a second class (the *memento*) that will hold all the necessary information, and when you want to serialise it, you create a new *memento* instance, copy all the information over and serialise the *memento* instance.Upon deserialising, you do it the opposite way.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
By submitting this form, you accept the Mollom privacy policy.


Navigation



Languages