Reputation: 131
Here Im trying to call a unknown List<T>
as a parameter in my createReports method on a button click. When I click button1
I need a List from CustomersInfo
class and when button2
is clicked I would like a List from ExpenseInfo
class.
Is this possible?
Code has been Updated.
class CustomersInfo
{
public string Name
{
get;
set;
}
public string Amount
{
get;
set;
}
}
class ExpenseInfo
{
public string Category
{
get;
set;
}
public string Name
{
get;
set;
}
public string Amount
{
get;
set;
}
}
List<CustomersInfo> customerInfo = new List<CustomersInfo>();
List<ExpenseInfo> expenseInfo = new List<ExpenseInfo>();
private void button1_Click(object sender,EventArgs e)
{
createReports(button1.Text, customerInfo);
}
private void button2_Click(object sender, EventArgs e)
{
createReports(button2.Text, expenseInfo);
}
viewForm.documentViewer1.CloseDocument();
string fileName = Application.StartupPath + "\\**\\report.docx";
if (File.Exists(fileName))
{
File.Delete(fileName);
}
try
{
var application = new Microsoft.Office.Interop.Word.Application();
var distinct = report.Distinct().GroupBy(x => x.Name).Select(y => y.First());
var document = application.Documents.Add(Template: Application.StartupPath + "/**/Templates/Reports.docx");
total = 0;
foreach (Microsoft.Office.Interop.Word.Field field in document.Fields)
{
if (field.Code.Text.Contains("Info"))
{
field.Select();
application.Selection.TypeText(Label);
}
else if (field.Code.Text.Contains("Grid"))
{
field.Select();
int RowCount = distinct.Count();
int ColumnCount = type.GetProperties().Length;
Object[,] DataArray = new object[RowCount, ColumnCount + 1];
//add rows
int r = 0;
int d = 0;
foreach (var client in distinct)
{
clientTotal = 0;
foreach (var info in report)
{
if (client.Name == info.Name)
{
clientTotal = Convert.ToDecimal(info.Amount.Remove(0, 1)) + clientTotal;
}
}
total = total + clientTotal;
DataArray[r, 0] = client.Name;
DataArray[r++, 1] = clientTotal.ToString("C2");
}
//page orintation
document.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;
dynamic oRange = document.Content.Application.Selection.Range;
string oTemp = "";
for (r = 0; r <= RowCount - 1; r++)
{
for (int c = 0; c <= ColumnCount - 1; c++)
{
oTemp = oTemp + DataArray[r, c] + "\t";
}
}
//table format
oRange.Text = oTemp;
object Separator = Microsoft.Office.Interop.Word.WdTableFieldSeparator.wdSeparateByTabs;
object ApplyBorders = true;
object AutoFit = true;
object AutoFitBehavior = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitFixed;
oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount,
Type.Missing, Type.Missing, ref ApplyBorders,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);
oRange.Select();
document.Application.Selection.Tables[1].Select();
document.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
document.Application.Selection.Tables[1].Rows.Alignment = 0;
document.Application.Selection.Tables[1].Rows[1].Select();
document.Application.Selection.InsertRowsAbove(1);
document.Application.Selection.Tables[1].Rows[1].Select();
//header row style
document.Application.Selection.Tables[1].Rows[1].Range.Bold = 1;
document.Application.Selection.Tables[1].Rows[1].Range.Font.Name = "Arial";
document.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 11;
//add header row manually
document.Application.Selection.Tables[1].Cell(1, 1).Range.Text = "Client";
document.Application.Selection.Tables[1].Cell(1, 2).Range.Text = "Amount";
//table style
document.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 1");
for (int c = 1; c <= document.Application.Selection.Tables[1].Rows.Count - 1; c++)
{
document.Application.Selection.Tables[1].Rows[c].Range.Font.Size = 9;
}
document.Application.Selection.Tables[1].Rows[1].Select();
document.Application.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
}
else if (field.Code.Text.Contains("total"))
{
field.Select();
application.Selection.TypeText("Total Income by Client " + total.ToString("C2"));
}
}
document.SaveAs(fileName);
document.Close();
application.Quit();
application = null;
document = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(st.FrameCount - 1);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
MessageBox.Show(line.ToString());
}
showReports(Label);
Any help would be greatly appreciated. Thanks in advance.
Upvotes: 0
Views: 2708
Reputation: 1
Hi I think you can use IEnumerable to handle this question
all the classes inherit from "object"
maybe you can try it as using
IEnumrable<object>
Upvotes: 0
Reputation: 903
Do you want both methods to call the same method because the objects are somehow related, i.e. share a common ancestor?
If all you need to do is iterate over the report, then look into IEnumerable, which supports covariance.
private void createReports(string Label, IEnumerable<BaseClass> report)
{
// foreach (var r in report)
// Do something....
}
Upvotes: 0
Reputation: 40393
Assuming you want Do Something
to behave differently depending on the type, you may want to have both classes implement an interface:
public interface IReportable {
string GetReportLine();
}
public class CustomersInfo : IReportable {
string IReportable.GetReportLine() {
// Get the line for your report
}
}
public class ExpenseInfo : IReportable {
string IReportable.GetReportLine() {
// Get the line for your report
}
}
private void CreateReports(string Label, IEnumerable<IReportable> report) {
foreach (IReportable info in report) {
string line = info.GetReportLine();
}
}
Upvotes: 1
Reputation: 19486
You can make createReports()
a generic method:
private void createReports<T>(string Label, List<T> report)
{
//Do something...
}
Upvotes: 4