2

I want to write a class for creating Excel files from Datagridviews. I want to add a bottom line with Subtotals for selected columns and the number of those columns change from 1 to 3. I want my function work like this

exportToExcel(string title, DataGridView dgv, int colId1, int colId2, int colId3); //for with 3 sub total 

//Example:
exportToExcel("Some Title",mygridview, 3);  //for 1 sub total
exportToExcel("Another Titme", othergridview, 5,6,7); //for 3 sub totals

Do I have to create functions for different variations or is there a way to make 2nd and 3rd column numbers optional ?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197

5 Answers5

6

You can use default values for the optional paramenters, something like:

exportToExcel(string title, DataGridView dgv, int colId1, int colId2 = 0, int colId3 = 0);

With a method like that you'd be able to make a call with 1 to 3 columns.

Also another approach would be this - the good thing about using this one is that if one day your number of columns increases or decreases you won't have to change the signature:

exportToExcel(string title, DataGridView dgv, params int[] colIds);
devcrp
  • 1,323
  • 7
  • 17
1

You can declare method like this. you have to provide the default value to the optional parameters.

void Method(int a, int b = 0, int c = 0)
{

}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
1

You can specify defaults for parameters in the method signature. Like this:

exportToExcel(string title, DataGridView dgv, int colId1, int colId2 = 6, int colId3 = 7);

Then, if you don't specify values when calling, then the "defaults" will be taken instead.

This means you can use this method for 3, 4, or 5 parameters specified.

exportToExcel("Some Title",mygridview, 3);  //Using 2 defaults
exportToExcel("Some Title",mygridview, 3, 6);  //using 1 default
exportToExcel("Another Titme", othergridview, 5,6,7); //full usage, no defaults
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
1

You can use list in the last parameter.

void exportToExcel(string str, object obj, List<int> subtotal){
//Based on Count - Logic Here
}

On Calling,

exportToExcel("Some Title",mygridview, new List<int>{3, 4, 5});
Gowthaman
  • 40
  • 5
1

You can assign default values to the optional parameter, only one thing you need to keep in mind is your optional parameters should define at the end of parameter list, after the required parameters.

public void ExportToExcel(string title, DataGridView dgv, int colId1, int colId2 = 0, int colId3 = 0)
{

   //Your business logic
}

You can call it like,

ExportToExcel("Excel work book", dgvObj, 1); //Value of colId1 = 1, colId2 = 0 and colId3 = 0
ExportToExcel("Excel work book", dgvObj, 1, 2); //Value of colId1 = 1, colId2 = 2 and colId3 = 0
ExportToExcel("Excel work book", dgvObj, 1, 2, 3); //Value of colId1 = 1, colId2 = 2 and colId3 = 3

You can use params int[] colIds to avoid multiple method parameters.

public void ExportToExcel(string title, DataGridView dgv, params int[] colIds)
{
   //You can access n parameters using index like colIds[0], colIds[1], colIds[2] 
   //Your business logic
}
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44