Question

I an writing an XML Writer and have it so that it can write this...

<?xml version="1.0" encoding="utf-8"?>
<mappings>
    <entry name="Iasanator">
        <item code="Red">He Needs Help</item>
    </entry>
</mappings>

...But I need to be able to put a varying number of id="whatever" statements passing into the XMLWriter.WriteHeader(# of these needs to scale) sub.

<header id="whatever" href="this" code="that" name="and so on".......>

I can put up to two thanks to overloads subs, but how do I put in like... 15 or something without writing separate overloads subs for each number of possible cases?

EDIT: Nkvu, I solved the problem on my own, then read that you had a similar idea. Great Minds Think Alike. I used a type of array like you suggested except I used a dictionary list where "parameters = New Dictionary(Of String, String)".

The entire XML Writer can be found at: http://pastebin.com/03TuX0Pd

If you find some of it confusing, (I'm not the best at commenting stuff) feel free to comment and ask a question. I'll be glad to answer.

Was it helpful?

Solution

Would you be able to post the VB.NET code that you have so far?

Broadly, I can think of two options:

Members with a Variable Number of Parameters

You might want something like this where you pass in a variable number of parameters to your sub and then it iterates over them and decides what to do with it.

Each parameter could be your header id (if it was a string or some base type like an integer) or you could pass in a class/complex type (e.g. you could have a Header class which has properties for the ID, href, code, name etc).

For the complex type, your signature would look something like:

Public Sub DoStuff(ParamArray list() as HeaderClass) 

And you would call it similar to:

DoStuff(headerObj1, headerObj2, headerObj3)

Where headerObj1 etc are objects which you have created previously

Pass in an array/list/collection

You could also just form a List or other collection type and pass it into your sub.

So your sub signature would look like:

Public Sub DoStuff(headers as List(of HeaderClass)) 

And you would call it similar to:

Dim headerList As List(Of HeaderClass) = New List(Of HeaderClass)
headerList.Add(headerObj1)
headerList.Add(headerObj2)
headerList.Add(headerObj3)

DoStuff(headerList)

Your sub could then iterate over the list and do what needs to be done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top