从函数中返回任何东西 [英] Returning Nothing from Function
问题描述
您好,
我正在尝试创建一个函数来测试TextBoxes并将Text转换为
DateTime如果不为空。
我的代码是:
公共共享函数GetDateTime(ByVal DateValue As String)As String
如果不是字符串.IsNullOrEmpty(DateValue)然后
返回DateTime.Parse(DateValue)
否则
不返回
结束如果
结束功能
然后在OnClick命令中我有:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
当有日期时它可以正常但如果它是空的我得到一条错误消息:
System.InvalidCastException:从字符串键入''日期''不是
有效。
如果我做CurrentMember.StartDate =没有什么工作正常所以没有问题
现场空值。
有没有人有任何建议,因为这已经困扰了我太久了。
谢谢提前
詹姆斯
Hello,
I am trying to create a Function to test TextBoxes and convert the Text to
DateTime if not empty.
My code is :
Public Shared Function GetDateTime(ByVal DateValue As String) As String
If Not String.IsNullOrEmpty(DateValue) Then
Return DateTime.Parse(DateValue)
Else
Return Nothing
End If
End Function
Then in a OnClick command I have:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
It works ok when there is a date but if its empty I get an error message:
System.InvalidCastException: Conversion from string "" to type ''Date'' is not
valid.
If I do CurrentMember.StartDate = Nothing it works fine so no problem with
Null values in the field.
Does anyone have any suggestions as this has puzzled me for too long now.
Thanks in advance
James
推荐答案
你可以试试这个。
公共共享功能GetDateTime( ByVal DateValue As String)As String
dim returnValue as DateTime = DateTime.MinValue
If Not String.IsNullOrEmpty(DateValue)then
DateTime.TryParse(DateValue,out returnValue)''''检查语法
这里,但它应该是正确的......我主要是c#now
结束如果
if(returnValue = DateTime.MinValue)那么
返回Nothing
结束如果
return returnValue
结束功能
或研究可空。
詹姆斯 < Ja *** @ discuss.microsoft.com写信息
news:81 ************************* ********* @ microsof t.com ...
You can try this.
Public Shared Function GetDateTime(ByVal DateValue As String) As String
dim returnValue as DateTime = DateTime.MinValue
If Not String.IsNullOrEmpty(DateValue) Then
DateTime.TryParse(DateValue, out returnValue ) ''''Check Syntax
here, but it should be right..I''m primarily c# now
End If
if ( returnValue = DateTime.MinValue ) then
return Nothing
end if
return returnValue
End Function
Or research "nullable".
"James" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.com...
您好,
我正在尝试创建一个函数来测试TextBoxes并将Text转换为
DateTime如果不为空。
我的代码是:
公共共享函数GetDateTime(ByVal DateValue As String)As String
如果不是String.IsNullOrEmpty(DateValue)那么
返回DateTime.Parse(DateValue )
否则
不返回
结束如果
结束功能
然后在OnClick命令中我有:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
它的工作原理好的,当有一个日期,但如果它是空的我收到一条错误消息:
System.InvalidCastException:从字符串"转换键入''日期''是
不
有效。
如果我做CurrentMember.StartDate =没什么可以正常工作所以没有问题
现场空值。
有没有人有任何建议,因为这让我困惑了太久了。
提前致谢
James
Hello,
I am trying to create a Function to test TextBoxes and convert the Text to
DateTime if not empty.
My code is :
Public Shared Function GetDateTime(ByVal DateValue As String) As String
If Not String.IsNullOrEmpty(DateValue) Then
Return DateTime.Parse(DateValue)
Else
Return Nothing
End If
End Function
Then in a OnClick command I have:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
It works ok when there is a date but if its empty I get an error message:
System.InvalidCastException: Conversion from string "" to type ''Date'' is
not
valid.
If I do CurrentMember.StartDate = Nothing it works fine so no problem with
Null values in the field.
Does anyone have any suggestions as this has puzzled me for too long now.
Thanks in advance
James
"詹姆斯" < Ja *** @ discuss.microsoft.com写信息
news:81 ************************* ********* @ microsof t.com ...
"James" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.com...
您好,
我正在尝试创建一个函数来测试TextBoxes并将Text转换为
DateTime如果不为空。
我的代码是:
公共共享函数GetDateTime(ByVal DateValue As String)As String
如果不是String.IsNullOrEmpty(DateValue)那么
返回DateTime.Parse(DateValue )
否则
不返回
结束如果
结束功能
然后在OnClick命令中我有:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
它的工作原理好的,当有一个日期,但如果它是空的我收到一条错误消息:
System.InvalidCastException:从字符串"转换键入''日期''是
不
有效。
如果我做CurrentMember.StartDate =没什么可以正常工作所以没有问题
现场空值。
有没有人有任何建议,因为这让我困惑了太久了。
在此先感谢
James
Hello,
I am trying to create a Function to test TextBoxes and convert the Text to
DateTime if not empty.
My code is :
Public Shared Function GetDateTime(ByVal DateValue As String) As String
If Not String.IsNullOrEmpty(DateValue) Then
Return DateTime.Parse(DateValue)
Else
Return Nothing
End If
End Function
Then in a OnClick command I have:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
It works ok when there is a date but if its empty I get an error message:
System.InvalidCastException: Conversion from string "" to type ''Date'' is
not
valid.
If I do CurrentMember.StartDate = Nothing it works fine so no problem with
Null values in the field.
Does anyone have any suggestions as this has puzzled me for too long now.
Thanks in advance
James
如果不是String.IsNullOrEmpty(DateValue)和DateValue< String.Empty然后
If Not String.IsNullOrEmpty(DateValue) AND DateValue <String.Empty Then
" James" < Ja *** @ discuss.microsoft.com写信息
news:81 ************************* ********* @ microsof t.com ...
"James" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.com...
您好,
我正在尝试创建一个函数来测试TextBoxes并将Text转换为
DateTime如果不为空。
我的代码是:
公共共享函数GetDateTime(ByVal DateValue As String)As String
如果不是String.IsNullOrEmpty(DateValue)那么
返回DateTime.Parse(DateValue )
否则
不返回
结束如果
结束功能
然后在OnClick命令中我有:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
它的工作原理好的,当有一个日期,但如果它是空的我收到一条错误消息:
System.InvalidCastException:从字符串"转换键入''日期''是
不
有效。
如果我做CurrentMember.StartDate =没什么可以正常工作所以没有问题
现场空值。
有没有人有任何建议,因为这让我困惑了太久了。
提前致谢
James
Hello,
I am trying to create a Function to test TextBoxes and convert the Text to
DateTime if not empty.
My code is :
Public Shared Function GetDateTime(ByVal DateValue As String) As String
If Not String.IsNullOrEmpty(DateValue) Then
Return DateTime.Parse(DateValue)
Else
Return Nothing
End If
End Function
Then in a OnClick command I have:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
It works ok when there is a date but if its empty I get an error message:
System.InvalidCastException: Conversion from string "" to type ''Date'' is
not
valid.
If I do CurrentMember.StartDate = Nothing it works fine so no problem with
Null values in the field.
Does anyone have any suggestions as this has puzzled me for too long now.
Thanks in advance
James
抱歉误读了你的帖子
函数GetDateTime(ByVal DateValue As String)As String
尝试
返回DateTime.Parse(DateValue)
Catch ex As Exception
不返回
结束尝试
结束功能
sorry misread your post
Function GetDateTime(ByVal DateValue As String) As String
Try
Return DateTime.Parse(DateValue)
Catch ex As Exception
Return Nothing
End Try
End Function
这篇关于从函数中返回任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!