c# Casting v As operator
-
True, but you should know beforehand if the casting is correct.
www.software-kinetics.co.uk Wear a hard hat it's under construction
Norm .net wrote:
True, but you should know beforehand if the casting is correct.
"Should" is a dangerous word. :) A plugin discovery mechanism (that makes extensive use of reflection) I recently built assumes nothing regarding the type of object that was loaded. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
B. Because apart from what others have mentioned in favor of b, it will automagically do a QueryInterface as well in case of a COM interface call.
A year spent in artificial intelligence is enough to make one believe in God
-
If a noob had asked that question in the Lounge they'd have been toast.
It's a debate, not a question.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
Sorry - wrong. The answer's a, because it's better.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
There seems to be alot of white space in your sig.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
B. Because apart from what others have mentioned in favor of b, it will automagically do a QueryInterface as well in case of a COM interface call.
A year spent in artificial intelligence is enough to make one believe in God
Good point, I suppose not many noobies would know about QueryInterface, ah those were the days. :)
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
Good point, I suppose not many noobies would know about QueryInterface, ah those were the days. :)
www.software-kinetics.co.uk Wear a hard hat it's under construction
Yeah... I always ended up using CComPtr to avoid reference-leak issues. MS should have banned direct use of QueryInterface and CoCreateInstance anyway. RAII should have been the way from onset.
A year spent in artificial intelligence is enough to make one believe in God
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
If an illegal cast is an exceptional (woah, that should never happen) situation, then (a) If there's a possibility that through normal use the cast might be invalid (e.g. Plug-in tpye code), then (b), followed by an If to check for Null. -Richard
Hit any user to continue.
-
Yeah... I always ended up using CComPtr to avoid reference-leak issues. MS should have banned direct use of QueryInterface and CoCreateInstance anyway. RAII should have been the way from onset.
A year spent in artificial intelligence is enough to make one believe in God
I used raw COM when the COM specs where first released, wow you needed a ton of code to do a small task. But it did give me background on the workings of COM.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
If an illegal cast is an exceptional (woah, that should never happen) situation, then (a) If there's a possibility that through normal use the cast might be invalid (e.g. Plug-in tpye code), then (b), followed by an If to check for Null. -Richard
Hit any user to continue.
Richard A. Dalton wrote:
Plug-in tpye code
That's the only situation I could eve think off, normally you should be aware if the cast would fail or not.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
I used raw COM when the COM specs where first released, wow you needed a ton of code to do a small task. But it did give me background on the workings of COM.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
It depends on what you're trying to do, the two things are different. If you're certain e is a SomeObject then I'd cast it. If it may or may not be I'd usually use the as keyword, in which case obj would be null if it isn't a SomeObject as opposed to throwing an exception.
-
There seems to be alot of white space in your sig.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Possibly a font-problem on your end? It looks roughly like: W**.'.** Balboos - the three punctuation-like characters being the HTML character code:
& there4;
(space put in after & so it doesn't render)"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
-
Oh that brings back memories, and forgetting idl files :)
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
Possibly a font-problem on your end? It looks roughly like: W**.'.** Balboos - the three punctuation-like characters being the HTML character code:
& there4;
(space put in after & so it doesn't render)"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
Not your name, your sig:
W∴ Balboos wrote:
Possibly a font-problem on your end?
It looks roughly like: W**.'.** Balboos - the three punctuation-like characters being the HTML character code:& there4;
(space put in after & so it doesn't render)"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
There's a lot of white space...
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
It's a debate, not a question.
www.software-kinetics.co.uk Wear a hard hat it's under construction
To be or not to be.? That is a question. :-D
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
To be or not to be.? That is a question. :-D
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Nope wrong, you've omitted the ? mark. ;)
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
Nope wrong, you've omitted the ? mark. ;)
www.software-kinetics.co.uk Wear a hard hat it's under construction
Did I?
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Did I?
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
The hand is quicker than the I eye.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
a. Throw back from C++ programming.
www.software-kinetics.co.uk Wear a hard hat it's under construction
Wrong - a is C-style casting, not C++
-
Wrong - a is C-style casting, not C++
Splitting hairs :)
www.software-kinetics.co.uk Wear a hard hat it's under construction