1

I am creating an application that parses an XML and retrieves some data. Each xml node specifies the data (const), a recordset's column-name to get the data from (var), a subset of possible data values depending on some condition (enum) and others. It may also specify, alongside the data, the format in which the data must be shown to the user.

The thing is that for each node type I need to process the values differently and perform some actinons so, for each node, I need to store the return value in a temp variable in order to later format it... I know I could format it right there and return it but that would mean to repeat myself and I hate doing so.

So, the question: How can I store the value to return, in a temp variable, while avoiding boxing/unboxing when the type is unknown and I can't use generics?

P.S.: I'm designing the parser, the XML Schema and the view that will fill the recordset so changes to all are plausible.


Update

I cannot post the code nor the XML values but this is the XML structure and actual tags.

<?xml version='1.0' encoding='utf-8'?>
<root>
    <entity>

        <header>
            <field type="const">C1</field>
            <field type="const">C2</field>

            <field type="count" />
            <field type="sum" precision="2">some_recordset_field</field> 

            <field type="const">C3</field>
            <field type="const">C4</field>
            <field type="const">C5</field>
        </header>

        <detail>
            <field type="enum" fieldName="some_recordset_field">
                <match value="0">M1</match>
                <match value="1">M2</match>
            </field>
            <field type="const">C6</field>

            <field type="const">C7</field>
            <field type="const">C8</field>
            <field type="var" format="0000000000">some_recordset_field</field>
            <field type="var" format="MMddyyyy">some_recordset_field</field>
            <field type="var" format="0000000000" precision="2">some_recordset_field</field>
            <field type="var" format="0000000000">some_recordset_field</field>
            <field type="enum" fieldName ="some_recordset_field">
                <match value="0">M3</match>
                <match value="1">M4</match>
            </field>
            <field type="const">C9</field>
        </detail>

    </entity>
</root>
PedroC88
  • 3,708
  • 7
  • 43
  • 77
  • 1
    If you could provide some sample code it would really help - it's a bit confusing at the moment. – Jon Skeet Feb 25 '11 at 16:46
  • more information would be good, what your saying is kind of: i need to do something for that thing – Fredou Feb 25 '11 at 16:54
  • You should use generics, but we need a piece of code. – xanatos Feb 25 '11 at 16:55
  • I wish I could... there are not generics in .net-1.1 – PedroC88 Feb 25 '11 at 16:59
  • Then live happy with boxing. If someone is paying you to program in .NET 1.1 in 2011, then he will be more than happy to live with boxing! :-) – xanatos Feb 25 '11 at 17:01
  • I don't think my boss cares about that... It's me who wants to avoid it. I like to go by the "recommended procedure" unless I got no choice ;) – PedroC88 Feb 25 '11 at 17:04
  • @PedroC88 Unless you can create a strong typed class where to put your partial numbers, I don't think you can do anything in 1.1 . The only thing you can do is preformat the data, so that you always have a string. But I think your problem isn't boxing or unboxing, is the fact that after unboxing you must rediscover the type to format it accordingly, right? If it's so, then you can put your value in an object with an overload ToString, so that when you recover it, you only have to call the virtual ToString. You could even put other methods in the class, that know how to manipulate the value. – xanatos Feb 25 '11 at 23:41
  • @xanatos Not really... Value types keep their foo.GetType() unchanged when boxed so even doubles know they're doubles. So the (.NET built in) Format function knows how to format it even if it is boxed into an object. – PedroC88 Mar 01 '11 at 13:55
  • @PedroC88 I thought you wanted to "overload" the ToString (format each type "by hand"). My reply was based on the idea that the standard ToString() isn't enough for you (or that you want to specify the ToString parameters for those types that support it) – xanatos Mar 01 '11 at 15:11
  • Nop... My problem is on casting multiple objects into a "generic" on .Net 1.1 – PedroC88 Mar 01 '11 at 15:22

1 Answers1

-1

Have you tried using the var type? That way you don't need to know the type of each node. Also, some small sample of your scenario would be useful.

Julio Casal
  • 276
  • 3
  • 13
  • Bwaaaaaah! var ISN'T that! It's a compiler shortcut. The compiler will assign the type during compilation. It's a static as a rock! – xanatos Feb 25 '11 at 16:54
  • 2
    And a link so can you know http://stackoverflow.com/questions/1552881/difference-between-var-and-object-in-c – xanatos Feb 25 '11 at 17:00