Click here to Skip to main content
15,902,839 members
Home / Discussions / C#
   

C#

 
AnswerRe: foreach Pin
DavidNohejl10-Feb-07 11:04
DavidNohejl10-Feb-07 11:04 
GeneralRe: foreach Pin
PIEBALDconsult10-Feb-07 15:12
mvePIEBALDconsult10-Feb-07 15:12 
QuestionConvert Image to Bitmap Pin
max2929710-Feb-07 10:15
max2929710-Feb-07 10:15 
AnswerRe: Convert Image to Bitmap Pin
mav.northwind10-Feb-07 10:34
mav.northwind10-Feb-07 10:34 
AnswerRe: Convert Image to Bitmap Pin
Stefan Troschuetz10-Feb-07 10:37
Stefan Troschuetz10-Feb-07 10:37 
AnswerRe: Convert Image to Bitmap Pin
Luc Pattyn10-Feb-07 10:42
sitebuilderLuc Pattyn10-Feb-07 10:42 
AnswerRe: Convert Image to Bitmap Pin
Christian Graus10-Feb-07 10:43
protectorChristian Graus10-Feb-07 10:43 
GeneralRe: Convert Image to Bitmap Pin
Guffa10-Feb-07 11:12
Guffa10-Feb-07 11:12 
Christian Graus wrote:
You should never do a direct cast. Do this instead

Bitmap bm = Bitmap.FromFile("Blah") as Bitmap;

the 'as' operator will return null if the object is not a valid target for the cast. A cast will just blow up.


Well, it's not always the best idea to postpone the error message as long as possible.

The code is more maintainable if an error occurs as close to the source as possible. If a cast doesn't work, it's easier to find the cause if the error message comes at the cast, than if it comes somewhere later in the code because of a reference mysteriously turning null.

Using as only serves a purpose if you actually check for null afterwards.

Also, in this case if the cast fails, you will lose the reference to the original object, which means that you won't be able to dispose it.

---
single minded; short sighted; long gone;

GeneralRe: Convert Image to Bitmap Pin
Christian Graus10-Feb-07 11:44
protectorChristian Graus10-Feb-07 11:44 
GeneralRe: Convert Image to Bitmap Pin
Stefan Troschuetz10-Feb-07 20:32
Stefan Troschuetz10-Feb-07 20:32 
GeneralRe: Convert Image to Bitmap Pin
Guffa11-Feb-07 0:10
Guffa11-Feb-07 0:10 
GeneralRe: Convert Image to Bitmap Pin
Stefan Troschuetz11-Feb-07 0:24
Stefan Troschuetz11-Feb-07 0:24 
GeneralRe: Convert Image to Bitmap Pin
Guffa11-Feb-07 11:27
Guffa11-Feb-07 11:27 
AnswerRe: Convert Image to Bitmap Pin
ali_reza_zareian10-Feb-07 10:55
ali_reza_zareian10-Feb-07 10:55 
GeneralRe: Convert Image to Bitmap Pin
Christian Graus10-Feb-07 11:00
protectorChristian Graus10-Feb-07 11:00 
GeneralRe: Convert Image to Bitmap Pin
ali_reza_zareian10-Feb-07 11:19
ali_reza_zareian10-Feb-07 11:19 
GeneralRe: Convert Image to Bitmap Pin
Luc Pattyn10-Feb-07 11:23
sitebuilderLuc Pattyn10-Feb-07 11:23 
GeneralRe: Convert Image to Bitmap Pin
ali_reza_zareian10-Feb-07 11:34
ali_reza_zareian10-Feb-07 11:34 
Questionc++ code on C# project Pin
Semion_N10-Feb-07 9:59
Semion_N10-Feb-07 9:59 
AnswerRe: c++ code on C# project Pin
Dave Kreskowiak10-Feb-07 10:42
mveDave Kreskowiak10-Feb-07 10:42 
AnswerRe: c++ code on C# project Pin
Christian Graus10-Feb-07 10:44
protectorChristian Graus10-Feb-07 10:44 
AnswerRe: c++ code on C# project Pin
Luc Pattyn10-Feb-07 11:49
sitebuilderLuc Pattyn10-Feb-07 11:49 
AnswerRe: c++ code on C# project Pin
Semion_N10-Feb-07 18:44
Semion_N10-Feb-07 18:44 
QuestionHyperbar Pin
Dirk Reske10-Feb-07 9:51
Dirk Reske10-Feb-07 9:51 
AnswerRe: Hyperbar Pin
Christian Graus10-Feb-07 10:46
protectorChristian Graus10-Feb-07 10:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.