|
Hi Everyone
Can someone explain to me why a nullable int cant be assigned the value of null e.g
int? accom = (accomStr == "noval" ? null : Convert.ToInt32(accomStr));
What's wrong with that code?
Thanks
Answer Snippets (Read the full thread at stackoverflow):
The problem is that both values returned == "noval" ? (int?)null : (int?)Convert.ToInt32(accomStr));
What Harry S stays is exactly right, but
int? accom....
The problem isn't that null cannot be assigned to an int?.
|
|
Consider the following statements:
int? v1 = null; int? v2 = 5 * v1;
What is the value of v2 ? ( null or empty string?)
How can I prevent the compiler to mark it as invalid operation? Do I need to follow custom exception handling?
Answer Snippets (Read the full thread at stackoverflow):
C# Language Specification 3.0 (Section §7.2.7: Lifted operators) For the binary ? modifier to each operand and result... .
int? v2 = v1.HasValue ? 5 * v1.Value : null;
or
int? v2 = nullIt's null .
Has a value prior to using it.
|
|
I was going through some code and came across a scenario where my combobox has not been initialized yet. This is in .NET 2.0 and in the following code, this.cbRegion.SelectedValue is null.
int id = (int)this.cbRegion.SelectedValue;
This code threw a null...
Started by Richard R on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Hence you're getting the null exception instead of a cast exception....
If you compile
object o = null; int a = (int)o;
and look at the MSIL code, you'll seeIt's attempting to read the object before it casts it.
The change to cast.
|
Ask your Facebook Friends
|
What is the best way to convert an int or null to boolean value in an SQL query, such that:
Any non-null value is TRUE in the results Any null value is FALSE in the results
Started by Thomas Bratt on
, 9 posts
by 9 people.
Answer Snippets (Read the full thread at stackoverflow):
When:
select (column_name is not null) as result from table_name;
Returns....
Isnull(column - column + 1, 0) != 0
SELECT CASE WHEN thevalue IS NULL THEN 0 ELSE 1 END AS newVal to be WHEN thevalue IS NULL THEN 0
No need to use case...
|
|
How can i get 0 as integer value from (int)null .
EDIT 1: I want to create a function that will return me default values for null representation in their respective datatypes.
EDIT 2: How can i work in this scenario for using default .
(int)Value
Where...
Started by Shantanu Gupta on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
What the default value of any data type:
int x = default(int); // == 0 string y = default(string); // == null....
You can cast it to an int? though.
You can't cast a null to an int, as an int is a value type.
|
|
Possible Duplicate:
Nullable types and the ternary operator. Why won’t this work?
Why doesn't this work? Seems like valid code. Can anyone help?
string cert = ddCovCert.SelectedValue; int? x = (string.IsNullOrEmpty(cert)) ? null: int.Parse(cert); Display...
Started by Kettenbach on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
I usually just cast the null to (int?)
int? x = (string.IsNullOrEmpty(cert)) ? (int?)null.
int? x = string.IsNullOrEmpty(cert) ? (int?)null : int.Parse(cert);
I've come across the same thing ...
|
|
Can an int be null in Java?
For example:
int data = check(Node root); if ( data == null ) { // do something } else { // do something }
My goal is to write a function which returns an int . Said int is stored in the height of a node, and if the node is...
Answer Snippets (Read the full thread at stackoverflow):
Depending on your check method, you never be null....
In Java, int is a primitive type and it is not considered Integer represents an int value, but it can hold a null value.
Args[]) { int i = null; }
Try to compile.
|
|
Say I have following the following snippet (context narrowed down to limit scope of question)
int? nullableId = GetNonNullableInts().FirstOrDefault();
Because GetNonNullableInts() returns ints, the FirstOrDefault will default to 0.
Is there a way to make...
Started by boris callens on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
So you'll need to....
int? nullableId = GetNonNullableInts().Cast<int?>().FirstOrDefault();
FirstOrDefault depends on T from IEnumerable<T> to know what type to return, that's why you're receiving int instead int? .
|
|
I have a linq query function like (simplified):
public IList<Document> ListDocuments(int? parentID) { return ( from doc in dbContext.Documents where doc.ParentID == parentID select new Document { ID = doc.ID, ParentID = doc.ParentID, Name = doc....
Started by Kelsey on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
When you explicitly test against null , the generated SQL will use the IS NULL operator, but when you're using a parameter it will use the standard....
Literal null values are handled differently than parameters which could be null.
|
|
Hello I hope someone can explain this problem. This is the code:
class Memory{ public: PacketPtr pkt; MemoryPort* port; MemCtrlQueueEntry(){};
};
And after I do:
std::list<Memory*>::iterator lastIter = NULL;
And I get the following error:
error:...
Started by Eduardo on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
If you want to assign NULL to the value at the location that the iterator; *listIter = NULL;
Initializing with list::end():
std::list<Memory *> aList; std::list<Memory*>.
That you cannot depend on.
|