Cast Syntax ?

  • Thread starter Thread starter cybertof
  • Start date Start date
C

cybertof

Hello,


What is the difference between those 2 lines.
Which one should be used ?


wsPublish = (Excel.Worksheet) wbPublish.Worksheets[1];

wsPublish = wbPublish.Worksheets[1] as Excel.Worksheet;



Regards,
Cybertof.
 
cybertof,

The first line has the possibility of throwing an exception if the value
returned from wbPublish.Worksheets[1] is not of type Excel.Worksheet (which
seems unlikely, given it seems that wbPublish is a workbook).

The second line will not throw an exception, rather, if
wbPublish.Worksheets[1] is not of type Excel.Worksheet, then it will place
null in wsPublish.

Hope this helps.
 
Back
Top