Can Casting ComObject Properties be problematic

30 Views Asked by At

So I dont know how Casting really works in detail

Working with excel comobjects I find it easily necessary to cast certain properties, because those properties come only back as Object

example:

_excel = New Excel.Application
_books = _excel.Workbooks
_book = _books.Open(path)
_sheets = _book.Worksheets
_sheet = DirectCast(_Sheets(0), Excel.Worksheet)  'This row
_range = _sheet.Range("A1","A20")
finallyNoComObjectAnymore = DirectCast(_range.Value(), Object(,)) 'This row
Marshal.FinalReleaseComObject(_range)
Marshal.FinalReleaseComObject(_sheet)
Marshal.FinalReleaseComObject(_sheets)
_book.Close
Marshal.FinalReleaseComObject(_book)
_books.Close
Marshal.FinalReleaseComObject(_books)
_excel.Quit
Marshal.FinalReleaseComObject(_excel)

Does Casting somehow interfere with the genuine pattern of retrieving and cleaning? Or does this in fact work just fine?

0

There are 0 best solutions below