Operator ?

  • Thread starter Thread starter MBSoftware
  • Start date Start date
M

MBSoftware

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
 
MBSoftware said:
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
 
Peter said:
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
 
On Apr 12, 3:38 pm, "Peter Duniho" <[email protected]>
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.  :)

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
 
Peter said:
What makes you think the code is production code in the first place?
How could changing non-production code "cause problems in production"?


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
 
Peter said:
That's right, he didn't.


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
 
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
 
Peter said:
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 :-)
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
 
Peter said:
You sure love your assumptions, don't you?


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
 
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.
 
Thanks a lot Pete you're a gentlemen.

Peter Duniho said:
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
 
Peter said:
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
 
"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___Rare_used_things.aspx

Note it describes ??

http://msdn2.microsoft.com/en-us/library/ms173224(VS.80).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."
 
Peter said:
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!
 
Bill said:
"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___Rare_used_things.aspx

Note it describes ??

http://msdn2.microsoft.com/en-us/library/ms173224(VS.80).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!;

?
 
Ben said:
Would that be:

x = a ??? b!;

or

x = ???! b;

or

x ?= ?? b!;

?

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)?
 
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
 
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.
 
Peter said:
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.


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!
 

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