我有一个可以为null的属性,我想返回一个空值.我怎么在VB.NET中这样做?
目前我使用这个解决方案,但我认为可能有更好的方法.
Public Shared ReadOnly Property rubrique_id() As Nullable(Of Integer)
Get
If Current.Request.QueryString("rid") <> "" Then
Return CInt(Current.Request.QueryString("rid"))
Else
Return (New Nullable(Of Integer)).Value
End If
End Get
End Property
Run Code Online (Sandbox Code Playgroud) 如何通过反射确定a Nullable(of Enum)是否确实是一个Enum?
我正在使用一种方法,该方法T使用IDataReader从数据库调用中检索的类型动态填充类型的对象.从本质上讲,它循环遍历datareader的序数,并且所有属性T和填充与序数名称匹配的属性(也会抛出一些属性魔法来更改列名称).在其他任何情况下,它都很有效,但是当我检查属性的时候BaseType,System.Enum我找到了,System.ValueType 因此,我的枚举检查失败了,方法就会爆炸.
[编辑:
Type.IsEnum我的工作方式不起作用.这里的主要问题是,TBaseType层次结构中没有任何内容表明它是一个Enum.这就好像使它成为一种Nullable类型会丧失我的Enum权利.]
有任何想法吗?
我试过这两个块,但它返回相同的值.我没有描述更多,我只是展示代码:
Dim f As Nullable(Of Integer)
If f = 1 Then
Console.WriteLine("Equal")
Else
Console.WriteLine("Not Equal")
End If
Run Code Online (Sandbox Code Playgroud)
它提示我"不等于"
我只是添加了一个NOT,我想要得到NOT答案,但我和上面一样!
Dim f As Nullable(Of Integer)
If Not f = 1 Then
Console.WriteLine("Equal")
Else
Console.WriteLine("Not Equal")
End If
Run Code Online (Sandbox Code Playgroud)
它正确地在C#中工作......
我有以下LINQ:
var items = from n in db.Roles
where n.importID == importID
select n;
Run Code Online (Sandbox Code Playgroud)
"importID"是一个字符串作为参数和一个字符串?对于数据库对象.如果在数据库中importID为null,则不匹配.怎么做到这一点?
编译器消息:
Operator '==' cannot be applied to operands of type 'string?' and 'string'
我在接受采访时得到了这个问题,现在我知道我给出了错误的答案.为什么这个程序在函数之间显示模糊的调用,因此没有编译?
public static void Display(int? num) {
Console.WriteLine(num);
}
public static void Display(string num) {
Console.WriteLine(num);
}
static void Main() {
Display(1);
Display("1");
Display(null);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud) 我有一个可以考虑的双倍
MyNullableDouble = MyDouble == 0 ? null : MyDouble;
Run Code Online (Sandbox Code Playgroud)
这引起了我一个问题:
无法确定条件表达式的类型,因为''和'double'之间没有隐式转换
我想做这样的事情:
int? l = lc.HasValue ? (int)lc.Value : null;
Run Code Online (Sandbox Code Playgroud)
其中lc是可以为空的枚举类型,比如EMyEnumeration?.所以我想测试lc是否有值,所以如果然后将其int值赋给l,否则l为null.但是当我这样做时,C#抱怨'无法确定条件表达式的错误类型,因为'int'和''之间没有隐式转换.
我怎样才能使它正确?提前致谢!!
我正在阅读一组结果,但遇到数据库可能返回类型的可空版本的问题,例如a double或int.
我想知道是否可以使用读者的架构信息将类型定义转换为可空版本.如?double?或int??
抛开所有SQL的东西,有没有办法一般地进行这种类型的转换?从一个Type物体到一个物体Nullable<Type>.
using (SqlConnection connection = new SqlConnection("... connection string here ..."))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = ".... some sql here ....";
var results = new DataTable(schema.TableName);
using (var reader = await command.ExecuteReaderAsync())
using (var schema = reader.GetSchemaTable())
{
for (int i = 0; i < schema.Rows.Count; i++)
{
var name = (string)schema.Rows[i]["ColumnName"];
var type = (Type)schema.Rows[i]["DataType"];
var allowNulls = (bool)schema.Rows[i]["AllowDBNull"];
if (allowNulls)
{ …Run Code Online (Sandbox Code Playgroud) 我有两个与DateTime分配相关的问题
DateTime? y = 1 == 1 ? null: DateTime.MaxValue;
DateTime? y = null; // assignment works as expected
Run Code Online (Sandbox Code Playgroud)
null和之间发生类型转换错误DateTime?哪个是DateTime?c#中空赋值的首选方法.
DateTime? x = default(DateTime?); //prints null on console
DateTime? x = null; // prints null on console
DateTime? x = DateTime.MinValue; //print 01/01/0001
Run Code Online (Sandbox Code Playgroud)我的ProcessDate类型是DateTime?当我使用ToString时它显示异常.
dfCalPlanDate.Text = concreteCalPlan.ProcessDate.ToString("d");
Run Code Online (Sandbox Code Playgroud)
谢谢你的兴趣.
nullable ×10
c# ×8
.net ×4
datetime ×2
vb.net ×2
clr ×1
enumeration ×1
enums ×1
if-statement ×1
int ×1
linq ×1
operators ×1
reflection ×1
sqlcommand ×1
tostring ×1