|
If you solved your problem
|
|
|
|
|
Maybe a recursive method that does the proper calculation based off the correct parameter input. Just be careful not to infinite loop yourself.
|
|
|
|
|
Hi All,
Can anyone tell me what the meaning of '?' in c# is?
Thanx.
|
|
|
|
|
It depends whereabouts it is.
1. In a variable definition, e.g. int? myValue = null; The ? means that the int is nullable (i.e. it can be set to null)
2. In a ternary operator, e.g. bool myVal = (item > 2 ? true : false); This reads, if item is greater than 2, then set myVal to true (otherwise set it to false).
3. As part of something called the null coalescing operator, e.g. int myVal = someValue ?? 1; This means, if someValue is not null, then set myVal to this value otherwise fallback to set it as 1.
|
|
|
|
|
Pete O'Hanlon wrote: bool myVal = (item > 2 ? true : false);
Pete O'Hanlon wrote: int myVal = someValue ?? 1;
the force has gone...
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
OK - and your problem here is?
|
|
|
|
|
After quite some time (> 2 years) of C# programming I still didn't know about the ?? operator, thanks for that
What Luc probably means is that bool myVal = (item > 2 ? true : false); can be simply expressed as bool myVal = item > 2 , so the example for the ternary operator probably wasn't the best.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Greeeg wrote: What Luc probably means is that bool myVal = (item > 2 ? true : false); can be simply expressed as bool myVal = item > 2, so the example for the ternary operator probably wasn't the best.
It was just a simple example. Judging by the level of the OP question, I figured this was about as much as he could handle. I rather suspect that Luc's objection has more to do with the fact that the ternary and coalescing operators actually lead to unclear code.
|
|
|
|
|
I think it's clear as anything else, I mean this operator is in most any C-style language: C#, Java, JavaScript, PHP, Ruby, Verilog, and others..
I have seen the ternary operator abused such as the following:
public bool myMethod()
{
bool myBool;
return (myBool==true)?(true) : ((myBool==false) ? false:true);
}
Greeeg wrote: I still didn't know about the ?? operator, thanks for that
Just so you know, unless you are interacting normal types with nullable types the ?? operator isn't worth much. It doesn't surprise me if you've never seen it before. It is one of those things that you won't know until you need it.
Edited to show I was talking to Greeeg about the ?? operator
modified on Monday, April 21, 2008 4:48 PM
|
|
|
|
|
SpacixOne wrote: Just so you know, unless you are interacting normal types with nullable types the ?? operator isn't worth much. It doesn't surprise me if you've never seen it before. It is one of those things that you won't know until you need it.
I suspect this comment was addressed at Greeg. Perhaps it should be put there.
SpacixOne wrote: I think it's clear as anything else, I mean this operator is in most any C-style language: C#, Java, JavaScript, PHP, Ruby, Verilog, and others..
The problem with the ternary operator is once you start using it, it's just so easy to overuse. I've seen code that has ternary operators nested inside ternary operators - all because somebody couldn't be bothered to type a couple of if/else statements in. Plus - newbies don't tend to know what it means.
|
|
|
|
|
Hi Pete,
there has been a horror thread not so long ago on the redundancy of myBool ? true : false
which just equals myBool
and IMO the other one does not compile, I expect ?? needs references, but your example
starts with int
Hence I must conclude the geordie beer has taken its toll.
After a good night sleep the force may be back though.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
OK - I was trying to put in a simple example for the ternary operator. I'm one of the haters of myBool ..., but it was such a trivial example that it didn't seem an issue here - oh well.
Luc Pattyn wrote: and IMO the other one does not compile, I expect ?? needs references, but your example
starts with int
Errm. It will compile - the ?? tells the compiler to fall through from the first value (if it's null) to the second one. Here's an example:
int? myValue = 2;
int itemVal = myValue ?? 10; The force is still strong with this one youngling.
|
|
|
|
|
Aha, the int? comes to the rescue.
I never got used to nullable types, I lived too long without them.
So I do like the ?? operator but the only usage I have for it is with references,
as in object obj=myObject??myDefaultObject;
I stand corrected by a true jedi
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Luc,
I'd like to know where you've come into the need of nullable types in C# other working with than ADO.NET of course
|
|
|
|
|
huh?
I just said I never got used to them since most of the time they have
not been available; we can live without, easily.
And I don't exactly like the extra code they generate nor the extra data memory
they cause either.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Oh I took it that you're not used to them yet because, well you are using them. My mistake then :/
|
|
|
|
|
Luc Pattyn wrote: I stand corrected by a true jedi
Strong in the force is the young one.
|
|
|
|
|
strung in the farce is the young one
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
There are 2. One is a conditional operation and the other is a Null-Coalescing operator.
1) Conditional
result = condition ? truepart : falsepart;
if(condition) result = truepart;
else result = falsepart;
2 Coalescing (evaluate b if a is null)
c = a ?? b
if(a == null) c = b;
else c = a;
*and the nullable type* Thanks Pete
Scott P
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
carbon_golem wrote: There are 2. One is a conditional operation and the other is a Null-Coalescing operator.
What about setting a nullable variable? BTW - the conditional is called the ternary operator.
|
|
|
|
|
I guess that's what I get, I forgot to take my umm... what's it called... that stuff that makes you remember better? darn it.
Thanks for the correction.
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
|
Pete O'Hanlon wrote: BTW - the conditional is called the ternary operator.
Which BTW is quite a silly name, as it only means "three operand operator", and doesn't say anything at all what the operator does with the operands.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
hy everyone!
i was wondering if there is a default string function to cut a string if it is to long to e.g. insert it into a textfield.
what i want to do:
i created a table which allows strings up to a maximum length. if it is too long i have to cut the rest.
lets say there is a string
"1,2,3,4,5,6,7,8,9,10" but the maximum length, which could be insert is 16 characters so the maximum string would be e.g. "1,2,3,4,5,6,7,8," but is there a chance to cut it to this length without having to parse it or without having to insert 16 characters one by one in a loop?
thanks.
stephan.
|
|
|
|
|
string s = "1,2,3,4,5,6,7,8,9,10";
string cut = s.Substring(0, 16);
so:
s = "1,2,3,4,5,6,7,8,9,10";
cut = "1,2,3,4,5,6,7,8,";
|
|
|
|
|