nullable

Conversion between Nullable types

北战南征 提交于 2019-12-01 04:02:24
Is there a converter in .NET 4.0 that supports conversions between nullable types to shorten instructions like: bool? nullableBool = GetSomething(); byte? nbyte = nullableBool.HasValue ? (byte?)Convert.ToByte(nullableBool.Value) : null; Not that I am aware of. You could just write a helper method like this: public Nullable<TTarget> NullableConvert<TSource, TTarget>( Nullable<TSource> source, Func<TSource, TTarget> converter) where TTarget: struct where TSource: struct { return source.HasValue ? (Nullable<TTarget>)converter(source.Value) : null; } Call it like this: byte? nbyte =

Fluent Nhibernate Automap convention for not-null field

北城以北 提交于 2019-12-01 04:01:36
Could some one help, how would I instruct automap to have not-null for a column? public class Paper : Entity { public Paper() { } [DomainSignature] [NotNull, NotEmpty] public virtual string ReferenceNumber { get; set; } [NotNull] public virtual Int32 SessionWeek { get; set; } } But I am getting the following: <column name="SessionWeek"/> I know it can be done using fluent-map. but i would like to know it in auto-mapping way. Thank you. Also, for reference properties ReferenceConvention need to be done. This is the code that works: public class ColumnNullConvention : IPropertyConvention {

History of VB.NET Nullable syntax

僤鯓⒐⒋嵵緔 提交于 2019-12-01 03:10:44
问题 I can't find a definitive answer. Since C# 2.0 you've been able to declare int? i = 125; as shorthand for Nullable<int> i = Nullable<int>(123); I recall reading somewhere that VB.NET did not allow this shortcut. But low and behold, I tried it in VS 2008 today and it works. Does anyone know whether it's been this way since .NET 2.0 or was this added later? 回答1: System.Nullable was introduced in .Net 2.0 and is available to VB as a generic type . You just cannot use the nullable syntax. So in

Nullable DateTime?

爱⌒轻易说出口 提交于 2019-12-01 03:10:13
how to create setter and getter Properties for nullable datetime. for example: private DateTime mTimeStamp; public DateTime TimeStamp { get { return mTimeStamp; } set { mTimeStamp = value; } } Does nullable attributes support setter and getter or have i to declare it public? private DateTime? mTimeStamp; public DateTime TimeStamp { } You can just do this instead: public DateTime? TimeStamp { get; set; } If you were having trouble with the compiler it's probably because you only changed one of the associated parts - either the private member variable or the property's data type. They need to

How to make a nullable field in a struct

强颜欢笑 提交于 2019-12-01 03:01:26
问题 I'm struggling to do table driven test, and I want do this: testCases := []struct { name string testUserID uint expected User // <- maybe nil expectedError error } Because the return values of tested function is *User, error . User is like this, it is defined as DB scheme. type User struct { ID uint CreatedAt time.Time UpdatedAt time.Time ... } But in this case, I cannot make expected nil. How can I do this? Or my approach to do table driven test is wrong? 回答1: For empty field you can check

NHibernate 2.* mapping files: how to define nullable DateTime type (DateTime?)?

家住魔仙堡 提交于 2019-12-01 02:34:35
I know one of the breaking changes with NHibernate 2.* is that the NHibernate.Nullables are no longer supported. Therefore, what do you use in your mapping file to map the nullable DateTime? type? For i.e.: Understandably doesn't work: <property name="CreateDate" column="CreateDate" type="DateTime?" not-null="false" /> And no longer supported: <property name="ModifiedDate" column="ModifiedDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" not-null="false"/> I know it must be so obvious, but I'm not finding it! Answer is as simple as: NHibernate will reflect over the

Is it possible to use operator ?? and throw new Exception()?

本秂侑毒 提交于 2019-12-01 02:07:36
I have a number of methods doing next: var result = command.ExecuteScalar() as Int32?; if(result.HasValue) { return result.Value; } else { throw new Exception(); // just an example, in my code I throw my own exception } I wish I could use operator ?? like this: return command.ExecuteScalar() as Int32? ?? throw new Exception(); but it generates a compilation error. Is it possible to rewrite my code or there is only one way to do that? For C# 7 In C# 7, throw becomes an expression, so it's fine to use exactly the code described in the question. For C# 6 and earlier You can't do that directly in

Where to put @Nullable on methods with nullable return types? [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-01 01:50:34
问题 This question already has answers here : Is it legal to put annotation after access modifier in Java 7? Or Java 8? (2 answers) Closed 3 years ago . When using javax.annotation.Nullable to mark methods as "potentially returning null value", where to put the @Nullable annotation? On the method, or return type? Is there any technical difference, or is this strictly a style issue? Style A: @Nullable public Object blah() { ... } Style B: public @Nullable Object blah() { ... } 回答1: This is strictly

Bug?? If you assign a value to a nullable integer via a ternary operator, it can't become null

こ雲淡風輕ζ 提交于 2019-12-01 00:26:22
dim val1 As Integer? = If(5 > 2, Nothing, 43) ' val1 = 0 dim val1 As Integer? = If(5 > 2, Nothing, Nothing) ' val1 = Nothing What gives? Is this a bug, or am I overlooking something? The problem is that Nothing in VB.NET works differently than, for example, null in C#. When Nothing is used in the context of a value type (such as Integer ) it represents the default value of that type. In this case, that's 0. In your first example, both branches of the ternary operator are valid Integer values. The true branch represents 0 and the false branch represents 43. In the second example, neither branch

Type Inference failed in a call to 'join' on nullable and non-nullable int

♀尐吖头ヾ 提交于 2019-12-01 00:17:24
问题 In my Linq, I am trying to make an inner join to a nullable field. Employee and Department have a relation, Department may have an EmployeeID or may have a null. So what would be my join, if i want only the records that satisifed the inner join (no result for null EmployeeIDs): var result = from emp in employees join dept in departments on new { Source = emp.EmployeeID } equals new { Source = dept.EmployeeID }; I am getting an exception: The type of one of the expressions in the join clause