473,473 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Operator ?

Someone can explain me the meanings of the ? operator applied to
declarations like that i found...

private Point? startPoint;

if i delete the ? the compiler gives me some errors but i don't understand
what's the use of ? in that declaration position...
help me please

Jun 27 '08 #1
38 1341
On Sat, 12 Apr 2008 15:18:22 -0700, MBSoftware <mb********@alice.itwrote:
Someone can explain me the meanings of the ? operator applied to
declarations like that i found...

private Point? startPoint;

if i delete the ? the compiler gives me some errors but i don't
understand what's the use of ? in that declaration position...
help me please
It's shorthand for "Nullable<Point>". Value type variables (e.g. structs
like Point) cannot normally be set to null. However, you can use a
"nullable" version to allow that.

You'll find that it can be a little inconvenient, due to the fact that to
get back to a regular value type you have to explicitly cast from the
nullable version to the regular version (e.g. "Point point =
(Point)startPoint;"). This means without the cast, the "nullable" trait
tends to propagate through your code as you copy values from one variable
to another. :)

But sometimes it makes sense to be able to treat a value type as
nullable. In that case, using the nullable version is just the ticket.

See:
http://msdn2.microsoft.com/en-us/library/1t3y8s4s.aspx
http://msdn2.microsoft.com/en-us/library/2cf62fcy.aspx

Also, a handy operator for resolving null-valued nullable types (e.g. to
provide a default value):
http://msdn2.microsoft.com/en-us/library/ms173224.aspx

Pete
Jun 27 '08 #2
MBSoftware wrote:
Someone can explain me the meanings of the ? operator applied to
declarations like that i found...

private Point? startPoint;

if i delete the ? the compiler gives me some errors but i don't
understand what's the use of ? in that declaration position...
help me please
Point is a structure and therefore a value type. Value types can not be
null. By adding ? to the declaration you make the type nullable. this is
usually used for values with a counterpart in a database which could be
null.

Personally i think it's not smart to delete code that you don't understand.

alain
Jun 27 '08 #3
On Sat, 12 Apr 2008 15:44:52 -0700, Alain Boss <ma******@hotmail.com>
wrote:
[...]
Personally i think it's not smart to delete code that you don't
understand.
Why not?

I mean, you wouldn't want to just delete it and move on. But as part of
trying to understand what the code is doing (which is what the OP was
doing), making changes to the code to see what happens, including deleting
portions of it to see what breaks, can be a very useful part of the
exploration process.

Seems like a very smart thing to do, quite the contrary to your own
thought on the matter.

Didn't you ever take things apart when you were a kid to try to figure out
how they worked?

Pete
Jun 27 '08 #4
Peter Duniho wrote:
On Sat, 12 Apr 2008 15:44:52 -0700, Alain Boss <ma******@hotmail.com>
>Personally i think it's not smart to delete code that you don't
understand.

Why not?

I mean, you wouldn't want to just delete it and move on. But as part of
trying to understand what the code is doing (which is what the OP was
doing), making changes to the code to see what happens, including
deleting portions of it to see what breaks, can be a very useful part of
the exploration process.
If there are close to perfect unit tests it may be OK.

If not then there is the risk of removing some code that was
needed and will cause problems in production.

Removing stuff that causes compile errors is best case not worst case.

Arne
Jun 27 '08 #5
On Sat, 12 Apr 2008 17:43:08 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
If there are close to perfect unit tests it may be OK.

If not then there is the risk of removing some code that was
needed and will cause problems in production.
What makes you think the code is production code in the first place? How
could changing non-production code "cause problems in production"?
Removing stuff that causes compile errors is best case not worst case.
For any of what you wrote to be relevant, you are making a number of broad
assumptions that you are entirely unjustified in making.

There is absolutely nothing in the original post that justifies saying
that the person who wrote that post or what he did is "not smart".
Neither you nor Alain have nearly enough information about the situation
to pass judgment like that.

Get off your high horses. The guy's just asking a question, trying to
learn how the language works. There's no reason for you to make a federal
case out of it.

Pete
Jun 27 '08 #6
On Apr 12, 3:38*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

<snip>
You'll find that it can be a little inconvenient, due to the fact that to *
get back to a regular value type you have to explicitly cast from the *
nullable version to the regular version (e.g. "Point point = *
(Point)startPoint;"). *This means without the cast, the "nullable" trait*
tends to propagate through your code as you copy values from one
variable *to another. *:)
I don't think I'd want it to behave in any other way - I certainly
wouldn't want an *implicit* conversion to a non-nullable point.

However, I personally tend to use the Value property instead of a cast
- it looks slightly tidier to me. It also makes it more obvious what's
going on if you try to do it to a null value. The cast just compiles
down to a call to Value, so the results are the same, but if you've
got a cast and you see a stack trace with get_Value in, you might
wonder what's going on.

Very much a personal style issue though.

Jon
Jun 27 '08 #7
Peter Duniho wrote:
On Sat, 12 Apr 2008 17:43:08 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
>If there are close to perfect unit tests it may be OK.

If not then there is the risk of removing some code that was
needed and will cause problems in production.

What makes you think the code is production code in the first place?
How could changing non-production code "cause problems in production"?
>Removing stuff that causes compile errors is best case not worst case.

For any of what you wrote to be relevant, you are making a number of
broad assumptions that you are entirely unjustified in making.

There is absolutely nothing in the original post that justifies saying
that the person who wrote that post or what he did is "not smart".
Neither you nor Alain have nearly enough information about the situation
to pass judgment like that.
The original poster did not say whether it was production code or not.

And then it makes sense to take the conservative approach and
assume that it is production code, because the damage of giving
"production code" advice to "just for fun code" is insignificant
to giving "just for fun code" advice to "production code".
Get off your high horses. The guy's just asking a question, trying to
learn how the language works. There's no reason for you to make a
federal case out of it.
He asked a question. Alan made a good point. You made a problem
out of it.

Arne

Jun 27 '08 #8
On Sat, 12 Apr 2008 18:48:53 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
The original poster did not say whether it was production code or not.
That's right, he didn't.
And then it makes sense to take the conservative approach and
assume that it is production code,
No, it doesn't make sense to "assume that it is production code".

If someone wanted to raise that as a possibility, and then provide a
caveat based on and qualified to that possibility, that would be one thing.

But to _assume_ that the OP is doing something stupid is, well...stupid.
As well as insulting to the OP.

Pete
Jun 27 '08 #9
On Sat, 12 Apr 2008 18:03:06 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
>You'll find that it can be a little inconvenient, due to the fact that
to Â*
get back to a regular value type you have to explicitly cast from the Â*
nullable version to the regular version [...]

I don't think I'd want it to behave in any other way - I certainly
wouldn't want an *implicit* conversion to a non-nullable point.
Nor I. I don't think what I wrote implied that I did, but if so, consider
this a clarification that it's not what I meant to say.

Not everything that's inconvenient is necessary bad.

Pete
Jun 27 '08 #10
On Sat, 12 Apr 2008 20:18:52 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[..]
Not everything that's inconvenient is necessary bad.
Or even "necessarily". :)
Jun 27 '08 #11
Peter Duniho wrote:
On Sat, 12 Apr 2008 18:48:53 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
>The original poster did not say whether it was production code or not.

That's right, he didn't.
>And then it makes sense to take the conservative approach and
assume that it is production code,

No, it doesn't make sense to "assume that it is production code".
Since you did not comment on the argument given:

# because the damage of giving
#"production code" advice to "just for fun code" is insignificant
#to giving "just for fun code" advice to "production code".

then I assume that you do not understand the argument.

Do you want me to it explain it in more detail ?
If someone wanted to raise that as a possibility, and then provide a
caveat based on and qualified to that possibility, that would be one thing.

But to _assume_ that the OP is doing something stupid is,
well...stupid. As well as insulting to the OP.
If the original poster plan on a career in software development,
then he better be prepared for that type of comments. There are
no room for "it is probably OK and let us not ask because someone
may feel offended" in software development.

Arne

Jun 27 '08 #12
On Sat, 12 Apr 2008 20:46:05 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
Since you did not comment on the argument given:

# because the damage of giving
#"production code" advice to "just for fun code" is insignificant
#to giving "just for fun code" advice to "production code".

then I assume that you do not understand the argument.
You sure love your assumptions, don't you?
Do you want me to it explain it in more detail ?
No. Since there was no need to make any assumptions at all, it doesn't
really matter what your "argument" in favor of the assumption you made
was. Your argument makes the assumption (let's see, looks like you're up
to at least three unwarranted assumptions already) that an assumption was
necessary, when in fact it wasn't. So your "argument" is moot.

That said, the fact is nothing in my reply to you is really for your
benefit. I already have enough experience dealing with you and your
insistence on assuming the worst about the person to whom you're replying
just so that you can make silly criticisms of their posts, that I know
there's nothing I could possibly write that would result in you admitting
a mistake and apologizing.

No, I wrote my reply mainly for the benefit of the OP, so that he
understands that not everyone reading this post is so ready to assume he's
done something stupid. In fact, many of us try to avoid making
assumptions at all. This newsgroup isn't nearly so hostile as one might
have guessed from yours and Alain's posts, and I hope he understands that
now.

Pete
Jun 27 '08 #13
No, I wrote my reply mainly for the benefit of the OP, so that he
understands that not everyone reading this post is so ready to assume
he's done something stupid. In fact, many of us try to avoid making
assumptions at all. This newsgroup isn't nearly so hostile as one might
have guessed from yours and Alain's posts, and I hope he understands
that now.
I didn't make any assumptions at all. Changing code that you don't
understand i usually not a good idea. Whether it's production code or
not doesn't really matter.

The OP asked a question, i answered it correctly. The last sentence was
introduced by the word 'personally'. It shows my opinion on this matter
and i will not discuss it here and I definetly don't want to hurt any
feelings.

But then, thanking for answers in a newsgroup, especially if they
answered your question, should be self-evident. IMHO asking questions
without thanking for responses is more hostile than what i said.

Actually, I probably feel more offended by your reaction than the OP by
my remark.

Alain
Jun 27 '08 #14
Peter Duniho wrote:
But to _assume_ that the OP is doing something stupid is,
well...stupid. As well as insulting to the OP.
I didn't assume anything. I wrote what i think and i'll stick to that
whether you like it or not. Then it was you who attacked arne and me for
our opinions. you even called me stupid. I have no idea who you are and
where you come from but statements like this could get you in trouble -
of course not with me :-)

In a previous post you wrote:
>I mean, you wouldn't want to just delete it and move on. But as part
of trying to understand what the code is doing (which is what the OP
was doing)
Well, this sounds like an assumption to me ... but then, let's finish
this debate before it's getting silly!

Alain
Jun 27 '08 #15
Peter Duniho wrote:
On Sat, 12 Apr 2008 20:46:05 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
>Since you did not comment on the argument given:

# because the damage of giving
#"production code" advice to "just for fun code" is insignificant
#to giving "just for fun code" advice to "production code".

then I assume that you do not understand the argument.

You sure love your assumptions, don't you?
>Do you want me to it explain it in more detail ?

No. Since there was no need to make any assumptions at all, it doesn't
really matter what your "argument" in favor of the assumption you made
was.
There are some people that are not interested in arguments.

But you have now confirmed that you did not understand the argument,
because the argument was not about an assumption.
Your argument makes the assumption (let's see, looks like you're
up to at least three unwarranted assumptions already) that an assumption
was necessary, when in fact it wasn't.
I only think I have made 1 assumption: that you did not understand
the argument I gave. And you have just confirmed that to be a true
assumption.

Arne
Jun 27 '08 #16
Ok i'm only a student so i deleted ? operator to discover what code does and
i think it's a good manner to learn programming...the code was a public
example of WPF program.
Thanks a lot for help.

"MBSoftware" <mb********@alice.itha scritto nel messaggio
news:95**********************************@microsof t.com...
Someone can explain me the meanings of the ? operator applied to
declarations like that i found...

private Point? startPoint;

if i delete the ? the compiler gives me some errors but i don't understand
what's the use of ? in that declaration position...
help me please
Jun 27 '08 #17
On Sun, 13 Apr 2008 13:32:40 -0700, MBSoftware <mb********@alice.itwrote:
Ok i'm only a student so i deleted ? operator to discover what code does
and i think it's a good manner to learn programming...the code was a
public example of WPF program.
I think it's a good way to learn too. There's nothing wrong with taking
some sample code and playing around with it, including deleting parts of
it, to see how those changes affect the results (compilation or execution).

On behalf of the newsgroup generally, I'm sorry that others disagree and
felt it necessary to criticize you for what is a perfectly normal and
reasonable approach to learning. Please don't let that discourage you,
and please feel free to post any other questions you may have. Most of us
are in fact here to help, and we look forward to future opportunities to
do so.

Pete
Jun 27 '08 #18
Thanks a lot Pete you're a gentlemen.

"Peter Duniho" <Np*********@nnowslpianmk.comha scritto nel messaggio
news:op***************@petes-computer.local...
On Sun, 13 Apr 2008 13:32:40 -0700, MBSoftware <mb********@alice.it>
wrote:
>Ok i'm only a student so i deleted ? operator to discover what code does
and i think it's a good manner to learn programming...the code was a
public example of WPF program.

I think it's a good way to learn too. There's nothing wrong with taking
some sample code and playing around with it, including deleting parts of
it, to see how those changes affect the results (compilation or
execution).

On behalf of the newsgroup generally, I'm sorry that others disagree and
felt it necessary to criticize you for what is a perfectly normal and
reasonable approach to learning. Please don't let that discourage you,
and please feel free to post any other questions you may have. Most of us
are in fact here to help, and we look forward to future opportunities to
do so.

Pete
Jun 27 '08 #19
Peter Duniho wrote:
On behalf of the newsgroup generally,
Oh - you are official spokesperson for the newsgroup ...
I'm sorry that others disagree and
felt it necessary to criticize you for what is a perfectly normal and
reasonable approach to learning.
As far as I can tell then you are the only one in this thread that
has been criticizing anyone ...

Arne
Jun 27 '08 #20
On Sun, 13 Apr 2008 18:53:49 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
Peter Duniho wrote:
>On behalf of the newsgroup generally,

Oh - you are official spokesperson for the newsgroup ...
Not official, no. Of course those readers of the newsgroup who feel that
yours and Alain's behavior was appropriate are free to disavow any
relation to my comments.
[...]
As far as I can tell then you are the only one in this thread that
has been criticizing anyone ...
Yes, it's plain enough that that's as far as you can tell. That's the
problem.

It's a too-common irony that the bully, when told to stop bullying, cries
that he himself is being bullied. The truth is, bullies often have
learned their behavior long ago, internalizing it to the point where they
are literally incapable of seeing the problem with their behavior.

You might consider therapy to help yourself with that. In the meantime,
you're just going to have to deal with being told to stop bullying others
and being bewildered as to why you've been told.

Pete
Jun 27 '08 #21
"Someone can explain me the meanings of the ? operator applied to
declarations like that i found... " ... snip ...

Hi,

Nullable types on MSDN :

http://msdn2.microsoft.com/en-us/library/1t3y8s4s.aspx

This article was valuable to me when I was transitioning to .Net 2.0 : (but
much more valuable was reading up on the operators it mentioned in the
latest version of Liberty's "Programming C#" and, of course, on MSDN) :

http://www.codeproject.com/KB/cs/C__...ed_things.aspx

Note it describes ??

http://msdn2.microsoft.com/en-us/lib...8VS.80%29.aspx

as well as ? Most of the usages it mentions are NOT new to .NET 2.0,
however, but, imho, are less well known.

How delightful that we have ? as the ternary operator (in-line if/then/else
short-hand), and now for nullable types, and ?? for selecting a non-null
value from two nullable operands.

I look forward to the inevitable coming of : ??? ! :)

best, Bill

"Never stop learning, never stop testing, never stop questioning."


Jun 27 '08 #22
Peter Duniho schrieb:
You might consider therapy to help yourself with that. In the meantime,
you're just going to have to deal with being told to stop bullying
others and being bewildered as to why you've been told.
Peter, what's your problem? You're the only bully in this thread. Just
read your posts again. Then count to four and close this thread.

Thank you!
Jun 27 '08 #23
Bill Woodruff wrote:
"Someone can explain me the meanings of the ? operator applied to
declarations like that i found... " ... snip ...

Hi,

Nullable types on MSDN :

http://msdn2.microsoft.com/en-us/library/1t3y8s4s.aspx

This article was valuable to me when I was transitioning to .Net 2.0
: (but much more valuable was reading up on the operators it
mentioned in the latest version of Liberty's "Programming C#" and, of
course, on MSDN) :

http://www.codeproject.com/KB/cs/C__...ed_things.aspx

Note it describes ??

http://msdn2.microsoft.com/en-us/lib...8VS.80%29.aspx

as well as ? Most of the usages it mentions are NOT new to .NET 2.0,
however, but, imho, are less well known.

How delightful that we have ? as the ternary operator (in-line
if/then/else short-hand), and now for nullable types, and ?? for
selecting a non-null value from two nullable operands.

I look forward to the inevitable coming of : ??? ! :)
Would that be:

x = a ??? b!;

or

x = ???! b;

or

x ?= ?? b!;

?
>
best, Bill

"Never stop learning, never stop testing, never stop questioning."

Jun 27 '08 #24
Ben Voigt [C++ MVP] wrote:
Bill Woodruff wrote:
>"Someone can explain me the meanings of the ? operator applied to
declarations like that i found... " ... snip ...

Hi,

Nullable types on MSDN :

http://msdn2.microsoft.com/en-us/library/1t3y8s4s.aspx

This article was valuable to me when I was transitioning to .Net 2.0
: (but much more valuable was reading up on the operators it
mentioned in the latest version of Liberty's "Programming C#" and, of
course, on MSDN) :

http://www.codeproject.com/KB/cs/C__...ed_things.aspx

Note it describes ??

http://msdn2.microsoft.com/en-us/lib...8VS.80%29.aspx

as well as ? Most of the usages it mentions are NOT new to .NET 2.0,
however, but, imho, are less well known.

How delightful that we have ? as the ternary operator (in-line
if/then/else short-hand), and now for nullable types, and ?? for
selecting a non-null value from two nullable operands.

I look forward to the inevitable coming of : ??? ! :)

Would that be:

x = a ??? b!;

or

x = ???! b;

or

x ?= ?? b!;

?
>best, Bill

"Never stop learning, never stop testing, never stop questioning."

How about ??? for, and I don't know the term for this although I believe
it has one, reading objects from properties/fields on objects, and then
reading yet another propert/field on that object, etc., but stop on the
first null-reference.

For instance:

TreeNode node = = node ??? Parent ??? Parent ??? Parent;

to get the grand-grandparent of the node, if available.

Basically the above code (and I've seen the construct, only with :
instead of ??? I think) could be translated to:

TreeNode node;
if (node != null && node.Parent != null && node.Parent.Parent != null &&
node.Parent.Parent.Parent != null)
node = node.Parent.Parent.Parent;
else
node = null;

But in lieu of the previous post, personally I like this particular
construct:

(bb | !bb)?
--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #25
On Apr 14, 8:24 am, Lasse Vågsæther Karlsen <la...@vkarlsen.nowrote:
How about ??? for, and I don't know the term for this although I believe
it has one, reading objects from properties/fields on objects, and then
reading yet another propert/field on that object, etc., but stop on the
first null-reference.
I know in Groovy it's called the safe-dereference operator - but I
like their syntax for it, which is ?. so your example would be:

node?.Parent?.Parent?.Parent

Note that the type of each expression doesn't have to be the same -
because the result will be null if any of the dereferencing fails, the
overall expression type can just be the type of the full expression
with normal dereferencing, providing it's a nullable type. For
instance:

string boss = employee?.Manager?.FullName;

where the types involved could be Employee, IManager, and string
respectively. That allows a handy conjunction with ?? as well:

string boss = employee?.Manager?.FullName ?? "Unknown";

Jon
Jun 27 '08 #26
On Mon, 14 Apr 2008 04:15:34 -0700, Alain Boss <ma******@hotmail.com>
wrote:
Peter, what's your problem? You're the only bully in this thread.
If it's being a bully to tell a bully to stop bullying, yes. I suppose
you might be right. Otherwise, you don't have a leg to stand on.
Just read your posts again. Then count to four and close this thread.
If that's such good advice, I'm wondering why you are giving it rather
than following it.
Jun 27 '08 #27
On Apr 12, 6:38 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
You'll find that it can be a little inconvenient, due to the fact that to
get back to a regular value type you have to explicitly cast from the
nullable version to the regular version (e.g. "Point point =
(Point)startPoint;"). This means without the cast, the "nullable" trait
tends to propagate through your code as you copy values from one variable
to another. :)
No need for a cast:
Point point = startPoint.Value;

Will work fine.. but you must test startPoint.HasValue first.
Jun 27 '08 #28
Peter Duniho wrote:
On Mon, 14 Apr 2008 04:15:34 -0700, Alain Boss <ma******@hotmail.com>
wrote:
>Peter, what's your problem? You're the only bully in this thread.

If it's being a bully to tell a bully to stop bullying, yes. I suppose
you might be right. Otherwise, you don't have a leg to stand on.
>Just read your posts again. Then count to four and close this thread.

If that's such good advice, I'm wondering why you are giving it rather
than following it.
Well, i guess it would have been much easier for you to count to 2
rather than 4. I'm still not sure what you think it was where i bullied
anybody. It was you who called me stupid, it was you who told arne to
make a therapy. The only thing i regret is that i reply to another of
your useless posts. mea culpa!
Jun 27 '08 #29
On Mon, 14 Apr 2008 15:35:51 -0700, Alain Boss <ma******@hotmail.com>
wrote:
Well, i guess it would have been much easier for you to count to 2
rather than 4.
Not satisfied with calling the OP "not smart", you now choose to insult me
as well? No doubt you'll not understand why this most recent post of
yours was offensive as well.
I'm still not sure what you think it was where i bullied anybody.
I don't understand why you aren't sure of "what I think it was where you
bullied anybody", since the flow of messages is quite clear. The first
post to which I replied is the obvious candidate. Why else would I have
made the reply I did? The later post in which you reiterated your
assertion would be a second example.
It was you who called me stupid,
No, I didn't. The only post in which I used that word, I was referring to
Arne's assumption that the OP is working with production code.

It was the assumption itself that I was calling stupid, not the individual
making it, and you claim to not even have made that assumption anyway so
it wouldn't apply to you in any case, however you interpreted it.
it was you who told arne to make a therapy.
Only because he, like you, is incapable of recognizing when someone else
has made a valid point that hurts his pride. And only after he insisted
on engaging in his own bullying. It's hardly bullying to recommend to
someone that they seek professional help when they are in fact apparently
in need of it and demonstrate that repeatedly over a significant period of
time.

In any case, whether or not I was bullying a couple of bullies has nothing
to do with whether they are bullies in the first place.
The only thing i regret is that i reply to another of your useless
posts. mea culpa!
Yes, it's your fault.

Pete
Jun 27 '08 #30
Yes, it's your fault.
:-) you are a crazy little man - 1,2,3,4
Jun 27 '08 #31
Well, you have to do that or compare to null with either the cast or
using the Value property.
No. The Value property will throw an exception when HasValue is false.
Anyway, the Value property is the base (non-nullable) type, which can never
be null, so comparison with null is impossible. Same for the result of the
cast.

You must check HasValue before casting or accessing the Value property. My
personal preference (for efficiency) is to check HasValue, then call
GetValueOrDefault() which does not throw, it is just a simple field access.
Jun 27 '08 #32
On Tue, 15 Apr 2008 06:39:01 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
>Well, you have to do that or compare to null with either the cast or
using the Value property.

No. The Value property will throw an exception when HasValue is false.
Anyway, the Value property is the base (non-nullable) type, which can
never
be null, so comparison with null is impossible. Same for the result of
the
cast.
You obviously misunderstood. Let me rephrase my statement so that you can
understand it:

"Whether you are using a cast or the Value property to retrieve the value,
you have to either do that (i.e. check the HasValue property) or compare
to null"
You must check HasValue before casting or accessing the Value property.
Or check to see if the value is null.
My personal preference (for efficiency) is to check HasValue, then call
GetValueOrDefault() which does not throw, it is just a simple field
access.
Holy premature optimizations, Batman! :)

Pete
Jun 27 '08 #33
Peter Duniho wrote:
On Tue, 15 Apr 2008 06:39:01 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
>>Well, you have to do that or compare to null with either the cast or
using the Value property.

No. The Value property will throw an exception when HasValue is
false. Anyway, the Value property is the base (non-nullable) type,
which can never
be null, so comparison with null is impossible. Same for the result
of the
cast.

You obviously misunderstood. Let me rephrase my statement so that
you can understand it:

"Whether you are using a cast or the Value property to retrieve the
value, you have to either do that (i.e. check the HasValue property)
or compare to null"
>You must check HasValue before casting or accessing the Value
property.

Or check to see if the value is null.
Value can't be null. Value is a non-nullable value type. You can't create
Nullable<Tfor a reference type T.
>
>My personal preference (for efficiency) is to check HasValue, then
call GetValueOrDefault() which does not throw, it is just a simple
field access.

Holy premature optimizations, Batman! :)

Pete

Jun 27 '08 #34
On Tue, 15 Apr 2008 16:13:23 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
Value can't be null.
Nowhere did I say it could be.
Value is a non-nullable value type.
I know that.
You can't create
Nullable<Tfor a reference type T.
So what?

Hopefully a simple code example will clear this up for you once and for
all:

int? i = null;
int j;

if (i.HasValue)
{
j = i.Value;
}

if (i.HasValue)
{
j = (int)i;
}

if (i != null)
{
j = (int)i;
}

if (i != null)
{
j = i.Value;
}

Frankly, after the first misunderstanding, I re-read my post and I agree
it wasn't quite as clear as it could have been. But after the second
misunderstanding, it's hard to understand why you can't put in a little
more effort on your own to see how, when interpreted in a particular way
(i.e. the way I _intended_), what I wrote isn't incorrect.

Pete
Jun 27 '08 #35
Frankly, after the first misunderstanding, I re-read my post and I
agree it wasn't quite as clear as it could have been. But after the
second misunderstanding, it's hard to understand why you can't put in
a little more effort on your own to see how, when interpreted in a
particular way (i.e. the way I _intended_), what I wrote isn't
incorrect.
I know exactly what you're trying to say, but a lot of the people reading
this newsgroup don't have our level of experience with C#, so we have to
write in a way that's correct and clear to them.

If you looked at my remarks *in context*, you would see exactly where the
ambiguity lies (using the word value to refer to something other that the
Value property).

Rather than (your words) "check to see if the value is null" let's just say
"compare the Nullable variable to null", then we obviously are not talking
about the Value property.
>
Pete

Jun 27 '08 #36
On Thu, 17 Apr 2008 06:26:50 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
I know exactly what you're trying to say, but a lot of the people reading
this newsgroup don't have our level of experience with C#, so we have to
write in a way that's correct and clear to them.
I saw no indication that anyone other than you had trouble understanding
what I wrote.

I don't disagree that it's important to be "correct and clear", but I have
no reason to believe that what I wrote was confusing anyone else.
If you looked at my remarks *in context*, you would see exactly where the
ambiguity lies (using the word value to refer to something other that the
Value property).
Yes, it's true. There's "value" and there's "Value property". I have no
trouble seeing the difference, and I would never write "value" if I meant
"Value property". All variables have a value, and I don't believe that
the difference should be confusing to anyone reading what I wrote.

And in any case, the first post to which you replied, the meaning of
"value" wasn't at issue anyway. The word wasn't even present in the text
you quoted.

Pete
Jun 27 '08 #37
And in any case, the first post to which you replied, the meaning of
"value" wasn't at issue anyway. The word wasn't even present in the
text you quoted.
Your actual words were "compare to null with either the cast or using the
Value property"

Here the direct object of the verb compare is missing (it wasn't specified
elsewhere in the sentence either), and the various possibilities for "What
is compared to null?" include "the cast" and "the Value property". But you
can't compare either of these to null, as we both very well know.
>
Pete

Jun 27 '08 #38
On Thu, 17 Apr 2008 10:44:29 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
>And in any case, the first post to which you replied, the meaning of
"value" wasn't at issue anyway. The word wasn't even present in the
text you quoted.

Your actual words were "compare to null with either the cast or using the
Value property"
Yes, and when read in context they are reasonably unambiguous and
understandable.

I understand that that's not how you read them. But if you do pay
attention to the rest of the text preceding that statement, the meaning is
plain.

I'm sorry you were confused, but I really don't think your comments have
added anything to this particular part of the discussion. You seem to be
the only person who was confused. I made an attempt to share some of
responsibility and to help clear up any misunderstanding you might have
had, but at this point you've used up my goodwill. I have the distinct
impression you're just harrassing me for the sake of it, which I don't
appreciate.

Pete
Jun 27 '08 #39

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.