Returning Nothing from Function

  • Thread starter Thread starter James
  • Start date Start date
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
 
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 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
 
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
 
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
 
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).

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
 
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:
 
Back
Top