Returning Nothing from Function

J

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
 
S

sloan

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".
 
T

ThatsIT.net.au

James said:
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


If Not String.IsNullOrEmpty(DateValue) AND DateValue <> String.Empty Then
 
T

ThatsIT.net.au

James said:
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

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
 
W

wisccal

Hi James,

Apparently, VB.Net converts Nothing to "". Therefore, I suggest you
declare your method to return a DateTime value as opposed to a string.
Nothing will then be converted into DateTime.MinValue (01/01/0001) as
you probably already noticed when you assigned Nothing directly to
CurrentMember.StartDate.

But to safeguard against strings that are invalid dates, I would also
incorporate Try/Catch or TryParse as recommended by the previous
posters.

==========
Regards,
Steve
www.stkomp.com
 
C

Cowboy \(Gregory A. Beamer\)

You can use nullable types, if using 2.0, but you will have to test the
value before you bind it, as Nothing/null blows up when bound to a control.

As far as conversion, .TryParse() is a safer method of setting a DateTime,
as users sometimes do not respect dates.

If you want to force dates, you can make the control so it cannot be filled
in and use a calendar control. Or you can use an AJAX masked edit (in the
AJAX control toolkit).
 
S

sloan

http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(VS.71).aspx
That's a try/finally block, not a try/catch or a try/catch/finally block.

Yes, you're' right, technet is full of try/finally examples.
But we aren't discussing try/finally blocks, we're discussing try/catch
blocks.

...


//thats a opinion only, I do not agree with it.//
Since Brad Abrams and Krzysztof Cwalina have probably forgotten more about
..Net then most people know (including myself), I'd probably value their
opinion more highly.




http://msdn2.microsoft.com/en-us/library/ms229005.aspx
http://msdn2.microsoft.com/en-us/library/ms229009.aspx







ThatsIT.net.au said:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top