I’ve searched around for the answer to this many times, but I don’t know what this type of method is called, so my searches ended up useless, nor can I explain it to the search engine.
How do you make a method that uses the variable it is appended to as it’s parameter (like “myVariable**.ToString()**)?”
.ToString() works on any variable I append it to, and uses the variable behind it as a parameter, rather than entering the parameter within the brackets… An example of what I want to do would be:
private void OpenExcel(string inFileName)
{
Excel.Application xlApp = new Excel.ApplicationClass();
Excel.Workbook xlBook = xlApp.Workbooks.Open(inFileName
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing);
Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
xlApp.Quit();
xlApp.releaseObject();
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception)
{
obj = null;
}
finally
{
GC.Collect();
}
}
I know this is kind of a stupid example, but it shows what I want to do with the “xlApp.releaseObject()”.
Also… Can someone please tell me what that kind of method (or type of call, whatever it is) is called? I hate not knowing.
They’re called extension methods and the only two changes that you would need to make to your method are to make it static and to add the keyword
thisto inFilename:The rest is compiler magic.