如何在vb.net中将对象项设置为null [英] how to set object item to null in vb.net
问题描述
大家好,
i有单选按钮
有2个值1,0是或否
如果用户选择否
i必须对象elments他们的值应为null
其中一个是datetime
rational_obj.OfficeWorkApproveDate =没有
和另一个一个是字符串
如何在vb.net中将它们置为空?
?
如果是一个字符串,你可以把它设置为Nothing
,因为String
是一个引用类型。
yourString = 没什么
但是对于值类型(和DateTime是一个),Nothing
在C#中给它相当于default(T)
,对于DateTime,这个是DateTime.MinValue
。如果要将其设置为Nothing
,请将OfficeWorkApproveDate
a 可空类型 [ ^ ](所以当你创建变量时,在变量名后添加一个问号)。
然后你可以给它Nothing
value:
rational_obj.OfficeWorkApproveDate = Nothing
稍后,如果你想测试它是否有值,试试这个:
< span class =code-keyword>如果 rational_obj.OfficeWorkApproveDate.HasValue 那么
Dim approveDate 作为 DateTime = rational_obj.OfficeWordApproveDate.Value
' 做操作机智h approveDate
否则
' 它没有值
结束 如果
hi all ,
i have radio button
with 2 values 1,0 yes or no
if user choose no
i have to object elments their values should be null
one of them is datetime
rational_obj.OfficeWorkApproveDate =nothing
and the other one is string
how can i put them to null in vb.net
?
If it's a string, you can just set it toNothing
, becauseString
is a reference type.
yourString = Nothing
But for a value type (and DateTime is one),Nothing
gives it the equivalent todefault(T)
in C#, and for DateTime, this isDateTime.MinValue
. If you want to set it toNothing
, makeOfficeWorkApproveDate
a nullable type[^] (so when you create the variable, add a question mark after the variable name).
Then you can give it theNothing
value:
rational_obj.OfficeWorkApproveDate = Nothing
And later, if you want to test whether it has a value, try this:
If rational_obj.OfficeWorkApproveDate.HasValue Then Dim approveDate As DateTime = rational_obj.OfficeWordApproveDate.Value ' do operations with approveDate Else ' it does not have a value End If
这篇关于如何在vb.net中将对象项设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!