nullable

Creating Nullables types in java

陌路散爱 提交于 2019-12-04 02:54:01
问题 I am trying to create a nullalble object in Java but no idea how to do this , in C# this would be done like this int? someTestInt; This allows me to check for for null , while in certain cases i can use a 0 value ,this isnt always possible since certain execution paths allow 0 values 回答1: I'm not entirely sure what you want, but if you want to have an integer value that also can be declared null , you probably want to use the Integer class: Integer nullableInteger = 1; nullableInteger = null;

Why does Nullable<T> not match as a reference type for generic constraints [duplicate]

淺唱寂寞╮ 提交于 2019-12-04 00:22:56
Possible Duplicate: Nullable type as a generic parameter possible? I came across a very weird thing with generic type constraints. I have a class like this: public SomeClass<T> where T:class { } However, I've found I can't use nullable types as I'd expect: new SomeClass<int?>(); I get an error that int? must be a reference type. Is Nullable really just a struct with syntactic sugar to make it look like a reference type? Nullable<T> is a struct (see MSDN ) however it is the only struct that does not satisfy the struct constraint. Therefore, you cannot use a Nullable as a generic type parameter

Assigning null/Nullable to DateTime in Ternary Operation

余生颓废 提交于 2019-12-04 00:02:10
I have a statement like DateTime ? dt = (string1 == string2) ? null; (DateTime)(txtbox.Text); which I cannot compile. Reason is : null cannot be assigned to DateTime . So, I have to declare a Nullable<DateTime> nullable variable and replace null with nullable . I do not want to use if -statement and I want to do this in one line. Also, Can I use operator ?? here. DateTime? dt = (string1 == string2) ? (DateTime?)null : DateTime.Parse(txtbox.Text); you can do it like that: DateTime ? dt = (string1 == string2) ? new Nullable <DateTime>(): (DateTime)(txtbox.Text); 来源: https://stackoverflow.com

MySQL ON DUPLICATE KEY UPDATE with nullable column in unique key

我们两清 提交于 2019-12-03 23:45:43
Our MySQL web analytics database contains a summary table which is updated throughout the day as new activity is imported. We use ON DUPLICATE KEY UPDATE in order that the summarization overwrites earlier calculations, but are having difficulty because one of the columns in the summary table's UNIQUE KEY is an optional FK, and contains NULL values. These NULLs are intended to mean "not present, and all such cases are equivalent". Of course, MySQL usually treats NULLs as meaning "unknown, and all such cases are not equivalent". Basic structure is as follows: An "Activity" table containing an

How scala generic constraints to nullable types work

心不动则不痛 提交于 2019-12-03 23:36:56
I've tried two ways to constrain a generic type parameter to a nullable type, but both seem to have some unexpected problems. First attempt (using T <: AnyRef): scala> def testAnyRefConstraint[T <: AnyRef](option:Option[T]):T = { | //without the cast, fails with compiler error: | // "found: Null(null) required: T" | option getOrElse null.asInstanceOf[T] | } testAnyRefConstraint: [T <: AnyRef](Option[T])T scala> testAnyRefConstraint(Some("")) res0: java.lang.String = scala> testAnyRefConstraint(Some(0)) <console>:16: error: inferred type arguments [Int] do not conform to method

Why can't nullables be declared const?

谁都会走 提交于 2019-12-03 23:36:23
[TestClass] public class MsProjectIntegration { const int? projectID = null; // The type 'int?' cannot be declared const // ... } Why can't I have a const int? ? Edit: The reason I wanted a nullable int as a const is because I'm just using it for loading some sample data from a database. If it's null I was just going to initialize sample data at runtime. It's a really quick test project and obviously I could use 0 or -1 but int? just felt like the right data structure for what I wanted to do. readonly seems like the way to go It's not just nullables; only types built into the runtime can be

Convert to a Nullable<T> from a string using Reflection

不羁岁月 提交于 2019-12-03 20:44:17
How can I convert TO a Nullable from a String using reflection? I have the following code to convert TO almost any value type given almost any value. There is quite a bit of code above this to use the IsAssignableFrom, etc. so this is the last resort. MethodInfo parse = t.GetMethod("Parse", new Type[] { typeof(string) }); if (parse != null) { object parsed = parse.Invoke(null, new object[] { value.ToString() }); return (T)parsed; } else { throw new InvalidOperationException("The value you specified is not a valid " + typeof(T).ToString()); } The problem comes when I want to to convert to a

Shielding nullable domain properties with ViewModel

末鹿安然 提交于 2019-12-03 17:08:00
I am using Entity Framework 4.0, and making use of POCO objects. When I populate POCO objects from the DB, I translate property values to my own Domain objects, which we can call my Model. Necessarily, whether or not the fields of my Model are Nullable depends on whether the value it maps to in the database comes from a NULL or NOT NULL column. I won't go into detail, but the values must be nullable in the DB, because a user can partially save a draft of the object before publishing it to the public. That being the case, I have several fields that are nullable. So let's say my model looks like

WCF - convert empty element to nullable native type

久未见 提交于 2019-12-03 16:59:50
Leaving a SOAP field element empty results in a cast error for native types. (sadly cannot use xsi:nil="true" due to client constraints) Marking the WCF contract native type as nullable<> does not appear to be enough to stop the following error being returned to the client. The string '' is not a valid Boolean value. at System.Xml.XmlConvert.ToBoolean(String s) at System.Xml.XmlConverter.ToBoolean(String value) System.FormatException does anyone know the best method of instructing the DataContractSerializer to convert empty elements to be deserialized to null? My example WCF service contract;

Whats the use of Nullable.GetUnderlyingType, if typeof(int?) is an Int32?

﹥>﹥吖頭↗ 提交于 2019-12-03 16:23:24
问题 why is typeof int? an Int32 int? x = 1; Console.WriteLine(x.GetType().Name); If it is okay then what's the use of Nullable.GetUnderlyingType ? 回答1: Calling GetType() boxes your variable. The CLR has a special rule that Nullable<T> gets boxed to T . So x.GetType will return Int32 instead of Nullable<Int32> . int? x = 1; x.GetType() //Int32 typeof(int?) //Nullable<Int32> Since a Nullable containing null will be boxed to null the following will throw an exception: int? x = null; x.GetType() /