如何在vb.net中将对象项设置为null [英] how to set object item to null in vb.net

查看:440
本文介绍了如何在vb.net中将对象项设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

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 to Nothing, because String is a reference type.

yourString = Nothing


But for a value type (and DateTime is one), Nothing gives it the equivalent to default(T) in C#, and for DateTime, this is DateTime.MinValue. If you want to set it to Nothing, make OfficeWorkApproveDate a nullable type[^] (so when you create the variable, add a question mark after the variable name).
Then you can give it the Nothing 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屋!

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