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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top