473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Returning Nothing from Function

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
Jun 27 '08 #1
10 1102
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...
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

Jun 27 '08 #2

"James" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.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

Jun 27 '08 #3

"James" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.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

Jun 27 '08 #4

You should not use Exception catching as a part of normal business flow.
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
I would stick with my TryParse version before I'd use the catch exception
version.
...

"ThatsIT.net.au" <me@workwrote in message
news:78**********************************@microsof t.com...
>
"James" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.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

Jun 27 '08 #5
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 <Ja...@discussions.microsoft.comwrote:
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
Jun 27 '08 #6
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" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.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

Jun 27 '08 #7

"sloan" <sl***@ipass.netwrote in message
news:uU**************@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/archi...16/396787.aspx
I would stick with my TryParse version before I'd use the catch exception
version.
..

"ThatsIT.net.au" <me@workwrote in message
news:78**********************************@microsof t.com...
>>
"James" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microso ft.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

Jun 27 '08 #8
"sloan" <sl***@ipass.netwrote in message
news:uU**************@TK2MSFTNGP04.phx.gbl...
You should not use Exception catching as a part of normal business flow.
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
Agreed 100%.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #9
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
"sloan" <sl***@ipass.netwrote in message
news:uU**************@TK2MSFTNGP04.phx.gbl...
>You should not use Exception catching as a part of normal business flow.
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx

Agreed 100%.

Technet is full of examples of doing just that

http://msdn2.microsoft.com/en-us/lib...nd(VS.71).aspx
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Jun 27 '08 #10
http://msdn2.microsoft.com/en-us/lib...nd(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" <me@workwrote in message
news:05**********************************@microsof t.com...
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
>"sloan" <sl***@ipass.netwrote in message
news:uU**************@TK2MSFTNGP04.phx.gbl...
>>You should not use Exception catching as a part of normal business flow.
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx

Agreed 100%.


Technet is full of examples of doing just that

http://msdn2.microsoft.com/en-us/lib...nd(VS.71).aspx
>--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sean | last post by:
HI, I would like to know if I can some set a variable to return a single value from a function using the set command. Is this possible? Thanks in advance for your answer Sean
11
by: Justin Naidl | last post by:
class Foo { protected: char foo_stuff; public: char* get_foo_stuff(); } Given the above example. What I want to know is the "proper/standard" way
4
by: i. dzhugashvili | last post by:
I'm hoping someone can help me here. I'm having problems using a function to get the search criteria for a column in the QBE grid. The column's data type is Long and the problem is that if I...
8
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling:...
4
by: Mark Kamoski | last post by:
Hi Everyone. Please help. Is there a way to return void from a Function? That is, what are the ramifications of writing something like the following...
5
by: Robert Fitzpatrick | last post by:
Can someone point me to some more information or perhaps show an example of returning a recordset from a plpgsql function. I'd like to send an argument or arguments to the function, do some queries...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
23
by: Peter | last post by:
I have a problem with a page show_image.asp that returns a jpg image under Windows XP Pro SP2. The page sets content type as: Response.ContentType = "image/jpg" While this works perfectly fine...
28
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.