Returning Nothing from Function

Returning Nothing from Function

am 19.04.2008 18:15:01 von 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

Re: Returning Nothing from Function

am 19.04.2008 18:27:58 von 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".





"James" wrote in message
news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@microsoft.com...
> 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

Re: Returning Nothing from Function

am 19.04.2008 18:29:30 von Premium Plastics

"James" wrote in message
news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@microsoft.com...
> 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

Re: Returning Nothing from Function

am 19.04.2008 18:37:35 von Premium Plastics

"James" wrote in message
news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@microsoft.com...
> 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

Re: Returning Nothing from Function

am 19.04.2008 20:03:48 von sloan

You should not use Exception catching as a part of normal business flow.


http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.asp x


I would stick with my TryParse version before I'd use the catch exception
version.


...





"ThatsIT.net.au" wrote in message
news:784FB9FB-E666-405A-A94F-DCC29935AA91@microsoft.com...
>
> "James" wrote in message
> news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@microsoft.com...
>> 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
>

Re: Returning Nothing from Function

am 19.04.2008 20:09:02 von Premium Plastics

"sloan" wrote in message
news:uUu6CdkoIHA.1164@TK2MSFTNGP04.phx.gbl...
>
> You should not use Exception catching as a part of normal business flow.

thats a opinion only, I do not agree with it.




>
>
> http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.asp x
>
>
> I would stick with my TryParse version before I'd use the catch exception
> version.
>
>
> ..
>
>
>
>
>
> "ThatsIT.net.au" wrote in message
> news:784FB9FB-E666-405A-A94F-DCC29935AA91@microsoft.com...
>>
>> "James" wrote in message
>> news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@microsoft.com...
>>> 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
>>
>
>

Re: Returning Nothing from Function

am 19.04.2008 20:39:21 von 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

On Apr 19, 9:15 am, James wrote:
> 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

Re: Returning Nothing from Function

am 19.04.2008 21:54:04 von NoSpamMgbworld

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!
|
*************************************************
"James" wrote in message
news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@microsoft.com...
> 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

Re: Returning Nothing from Function

am 20.04.2008 17:40:54 von mark

"sloan" wrote in message
news:uUu6CdkoIHA.1164@TK2MSFTNGP04.phx.gbl...

> You should not use Exception catching as a part of normal business flow.
> http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.asp x

Agreed 100%.


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: Returning Nothing from Function

am 21.04.2008 10:41:47 von Premium Plastics

"Mark Rae [MVP]" wrote in message
news:%23%238KqzvoIHA.4716@TK2MSFTNGP06.phx.gbl...
> "sloan" wrote in message
> news:uUu6CdkoIHA.1164@TK2MSFTNGP04.phx.gbl...
>
>> You should not use Exception catching as a part of normal business flow.
>> http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.asp x
>
> Agreed 100%.
>
>

Technet is full of examples of doing just that

http://msdn2.microsoft.com/en-us/library/system.data.sqlclie nt.sqlcommand(VS.71).aspx



> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net

Re: Returning Nothing from Function

am 21.04.2008 15:53:18 von sloan

http://msdn2.microsoft.com/en-us/library/system.data.sqlclie nt.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" wrote in message
news:0502FA77-0E67-441D-A328-F818D8A50B9C@microsoft.com...
> "Mark Rae [MVP]" wrote in message
> news:%23%238KqzvoIHA.4716@TK2MSFTNGP06.phx.gbl...
>> "sloan" wrote in message
>> news:uUu6CdkoIHA.1164@TK2MSFTNGP04.phx.gbl...
>>
>>> You should not use Exception catching as a part of normal business flow.
>>> http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.asp x
>>
>> Agreed 100%.
>>
>>
>
> Technet is full of examples of doing just that
>
> http://msdn2.microsoft.com/en-us/library/system.data.sqlclie nt.sqlcommand(VS.71).aspx
>
>
>
>> --
>> Mark Rae
>> ASP.NET MVP
>> http://www.markrae.net
>