Filipe Costa
Filipe Costa

Reputation: 555

pass diferent parameters to a function

I am working with documents, trying to create a docx via a web page, basicly i say i want a table, a paragraph and it should append to my doc, at the backgroud i have a function that deals witht that stuff this way:

public void CreateDoc(params object[] document)
{
    var stream = new MemoryStream();
    using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
    {
        MainDocumentPart mainPart = doc.AddMainDocumentPart();

        new Document(new Body()).Save(mainPart);

        Body body = mainPart.Document.Body;

        foreach (var docSections in document)
        {
           body.Append(new Paragraph(new ParagraphProperties(),
           new Run((Run)docSections)));
        }

    }
    stream.Seek(0, SeekOrigin.Begin);
    Directory.CreateDirectory(HostingEnvironment.MapPath(DOCUMENTSLOCATION));
    System.IO.File.WriteAllBytes(HostingEnvironment.MapPath("~/Files/test5.docx"), stream.ToArray());



  }

watch out the parameters received in the CreateDoc, it can receive a unlimited number of paramters inside the object, that is what i want since i don'tn know how much sections my doc will have (tables, paragraphs, images etc..)-

My problem is that i need t pass more parameters that are not directly related to the construction of the file, like for example: name of the file. My idea was to pass it as the second parameter in the CreateDoc function, but how can i pass it from the other side?

Any help?

Upvotes: 0

Views: 44

Answers (2)

PurTahan
PurTahan

Reputation: 983

For First Step:

You Can pass Data with your own Structure instead Object.
Like Data Type and Data Content

And Second Step:

Check DataType of Parameter and then Manage it

Upvotes: 0

Nisarg Shah
Nisarg Shah

Reputation: 14541

You can always pass specific parameters to function if you have defined them as such. For instance, if you are certain that you'll always need filename as a parameter while calling this function, you can modify the function parameter as such:

public void CreateDoc(string filename, params object[] document)
{
   ...
}

Then you could call it like this:

CreateDoc("filename.docx", <param1>, <param2>);

Upvotes: 1

Related Questions