为分配一个值,而不是为null [英] assigning a value for instead of null

查看:86
本文介绍了为分配一个值,而不是为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,
如果变量即将为null,而没有使用if语句,则赋值的语法是什么?

问题是

Dears,
What is the syntax of assigning a value if the variable is coming null, without using the if statement,

The problem is that

RADentryDate.SelectedDate = DateTime.Parse(row[15].ToString())


有时row[15] is DBNULL

问题是当它为null时,会发生错误.所以我想在值变为空时分配一个默认日期

问候


sometimes the row[15] is DBNULL

The problem is that when it is null, an error occurs. so i want to assign a default date when the value comes null

Regards

推荐答案

如果您要谈论的是从数据库获取可能为null的值,则可以执行以下操作

If you are talking about getting values from a database that might be null you could do something like this

string name = datareader.IsDBNull(0) ? "Unknown Name" : datareader.GetString(0);



否则,您需要详细说明问题.



Otherwise you need to elaborate your question some more.


我认为您不能逃避if,但是可以使用速记语法:
I don''t think you can escape from if, but you can use the shorthand syntax for it:
// maybeNull is the passed parameter - if it''s not null it will be assigned to someVariable, else use valueToAssignIfNull
var someVariable = maybeNull ?? valueToAssignIfNull;


我不是c#专业人士,但我会尝试:

I am no c# pro but I''d try:

RADentryDate.SelectedDate = !DBNull.Value.Equals(row[15]) ? DateTime.Parse(row[15].ToString()) : DateTime.Now;


这篇关于为分配一个值,而不是为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆