我已经有这个了:
public static object GetDBValue(object ObjectEvaluated)
{
if (ObjectEvaluated == null)
return DBNull.Value;
else
return ObjectEvaluated;
}
used like:
List<SqlParameter> Params = new List<SqlParameter>();
Params.Add(new SqlParameter("@EntityType", GetDBValue(EntityType)));
Run Code Online (Sandbox Code Playgroud)
现在我想保留相同的接口,但扩展它以将其与可为空的一起使用
public static object GetDBValue(int? ObjectEvaluated)
{
if (ObjectEvaluated.HasValue)
return ObjectEvaluated.Value;
else
return DBNull.Value;
}
public static object GetDBValue(DateTime? ObjectEvaluated)
{...}
Run Code Online (Sandbox Code Playgroud)
但我只想要 1 个函数 GetDBValue 用于空值。我该如何做到这一点并保持通话原样?这可能吗?
我可以让它像这样工作:
public static object GetDBValue<T>(Nullable<T> ObjectEvaluated) where T : struct
{
if (ObjectEvaluated.HasValue)
return ObjectEvaluated.Value;
else
return DBNull.Value;
}
Run Code Online (Sandbox Code Playgroud)
但调用更改为:
Params.Add(new SqlParameter("@EntityID ", GetDBValue<int>(EntityID)));
Run Code Online (Sandbox Code Playgroud) 如果模型字段为空,我想打印“null”。我的领域
public int? PostHouseNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
在cshtml中
@(item.PostHouseNumber == null ? "null" : item.PostHouseNumber)
Run Code Online (Sandbox Code Playgroud)
如何强制转换int?串起来?
我的方法是...
Autodesk.AutoCAD.Geometry.Point3d point = null;
Run Code Online (Sandbox Code Playgroud)
但我似乎无法设置 a Point3dto null。
谁能告诉我为什么以及如何检查 a 是否Point3d是null?
(Point3D我的意思是Autodesk.AutoCAD.Geometry.Point3d)
我认为我遗漏了一些关于可空类型的基本知识。希望这个例子能够开启新的理解,但至少,也许我们可以让这件事顺利进行。
在类(对话框形式)中,我声明:
Property ProductStructureHeaderKey As Int32?
Run Code Online (Sandbox Code Playgroud)
在另一个类中,我声明该对话框的一个实例,并尝试使用以下行设置该属性:
dr.ProductStructureHeaderKey = If(parentRow.Cells(1).Value Is Nothing, Nothing, Int32.Parse(parentRow.Cells(1).Value))
Run Code Online (Sandbox Code Playgroud)
当该行将 Nothing 分配给属性时,该属性等于 0。(然后,当我希望它传递 NULL 时,它会将 0 传递给数据库。)
这不是我所期望的,我一直在寻找代码(SO、MSDN 等),看起来我做的事情是正确的,但显然,我没有。那么,朋友们,我做错了什么?如何使用可空类型来满足我的需求?
我在 color.xml 中制作了一个包含 12 种不同颜色的颜色数组。但在我尝试提取颜色并在代码中使用它们时,数组中的所有值都为 null。我还尝试使用 TypedArray 解决方案,没有任何区别。那么有什么问题吗?
public void testColor(){
Resources resources = App.getAppContext().getResources();
String colors[] = resources.getStringArray(R.array.backgroundcolors);
//prints null
Log.d("TAG", " " + colors[3]);
//prints 12x null
for(String x : colors){
Log.d("TAG", " " + x);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testColor();
}
Run Code Online (Sandbox Code Playgroud)
颜色.xml
<array name="backgroundcolors">
<item>#000000</item>
<item>#373737</item>
<item>#ffffff</item>
<item>#e6e6e6</item>
<item>#EAE1D8</item>
<item>#fd79a1</item>
<item>#ff0f68</item>
<item>#E849A1</item>
<item>#F7E84E</item>
<item>#FFB732</item>
<item>#48B1E3</item>
<item>#5dd95d</item>
</array>
Run Code Online (Sandbox Code Playgroud) 可以将可空字段定义为 sortkey 和 distkey 吗?我预计不会有很多空值,但它可能会发生。谢谢
我想知道为什么 Visual Studio 2019 没有抱怨这段 C# 代码:
dynamic deserializedBody = JsonConvert.DeserializeObject(requestBody);
deserializedBody?.date.ToString();
Run Code Online (Sandbox Code Playgroud)
既然deserializedBody?.date可能null,就意味着该ToString方法将应用于某事null。我认为是这样的:
deserializedBody?.date?.ToString();
Run Code Online (Sandbox Code Playgroud)
将是正确的使用形式,但 Visual Studio 不会抱怨第一个版本。当然,我错过了关于这段代码的真实性质的一些东西。
例子来自Kotlin官网
val a: Int = 100
val boxedA: Int? = a
val anotherBoxedA: Int? = a
val b: Int = 100
val boxedB: Int? = b
val anotherBoxedB: Int? = b
println(a === a) // true
println(boxedA === anotherBoxedA) // true
println(boxedB === anotherBoxedB) // true
Run Code Online (Sandbox Code Playgroud)
我理解了上面的例子。但是,当我的值更改一个和b从100到1000,输出变为假的真象下面这样:
val a: Int = 1000
val boxedA: Int? = a
val anotherBoxedA: Int? = a
val b: Int = 1000
val …Run Code Online (Sandbox Code Playgroud) 这是我的Foo数据类定义
data class Foo(
var fooArg: Array<Int?>? = null,
)
Run Code Online (Sandbox Code Playgroud)
这是对它的调用:
val bar: Array<Int> = arrayOf(1,2,3)
val foo = Foo(fooArg = bar)
Run Code Online (Sandbox Code Playgroud)
但这给出了一个错误type mismatch: required Array<Int?>? but found Array<Int>
我很困惑,它需要一个可为空的类型,而我为它提供了一个非空值,该类型如何不匹配?
string我有一个方法可以接收和的字典object
private void MyFunc(Dictionary<string, object> parameters)
{
}
Run Code Online (Sandbox Code Playgroud)
我有一个用例,我需要调用对象为可为空 Guid 的方法
Nullable<Guid> guid = Guid.NewGuid();
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("myGuid", guid);
MyFunc(parameters);
Run Code Online (Sandbox Code Playgroud)
当我尝试获取对象的类型时,它似乎是 System.Guid
parameters["myGuid"].GetType()
Run Code Online (Sandbox Code Playgroud)
我如何知道该对象是否可为空?当将对象作为方法参数发送时,我是否会丢失可为空的值?