473,545 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Optional ByVal SomeDate As Date = Nothing" in VB2005

In our applications, we use the special value of DateTime.MinVal ue to
represent "null dates" throughout all our code. We recently ran into an
issue where we wanted an optional date parameter for a procedure. We weren't
able to declare it with DateTime.MinVal ue as its default value, as MinValue
is a read-only property rather than a constant. To work around, we had to
use a "magic date" that we checked for later on. I was never very happy with
that solution.

Today however I noticed that VB2005 is quite happy with the following:

\\\
Public Sub DoSomething(Opt ional ByVal SomeDate As Date = Nothing)
[...]
End Sub
///

(I believe this fails to compile in VB2003).

The value that is present in SomeDate if no date is provided by the calling
code is DateTime.MinVal ue (i.e., the default value for an uninitialised date
variable).

Is this the intended documented behaviour of declaring the optional
parameter in this way? Can I use this safe in the knowledge that the
behaviour won't break or change in the next release of VB?

Thanks,

--

(O)enone
Jun 15 '06 #1
2 11900
"Oenone" <oe****@nowhere .com> schrieb:
In our applications, we use the special value of DateTime.MinVal ue to
represent "null dates" throughout all our code.
I don't think this is an optimal solution. Some alternative approaches are
demonstrated at <URL:http://dotnet.mvps.org/dotnet/articles/nullabledates/>
(just ignore the text and check out the code snippets).
Today however I noticed that VB2005 is quite happy with the following:

\\\
Public Sub DoSomething(Opt ional ByVal SomeDate As Date = Nothing)
[...]
End Sub
///

(I believe this fails to compile in VB2003).

The value that is present in SomeDate if no date is provided by the
calling code is DateTime.MinVal ue (i.e., the default value for an
uninitialised date variable).
The value of 'DateTime.MinVa lue' is specified (see documentation).
Is this the intended documented behaviour of declaring the optional
parameter in this way? Can I use this safe in the knowledge that the
behaviour won't break or change in the next release of VB?


Yes, it is.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jun 15 '06 #2
Oenone,

As Herfried suggests I would use a Nullable(Of DateTime) in .NET 2.0 to
represent a "null date" (or one of the other methods in .NET 1.x) rather
then a "special value".

| Public Sub DoSomething(Opt ional ByVal SomeDate As Date = Nothing)
| [...]
| End Sub
|
| (I believe this fails to compile in VB2003).
It fails to compile in VB 2003, which feels like a bug as this is allowed:

Dim empty As DateTime = Nothing

| Is this the intended documented behaviour of declaring the optional
| parameter in this way?
Maybe ;-)

Rather then use an optional parameter I would use an overloaded method.
Considering that Nullable(Of T) cannot be optional...

Public Sub DoSomething()
DoSomething(Not hing)
End Sub

Public Sub DoSomething(ByV al SomeDate As Nullable(Of Date))

End Sub

| Can I use this safe in the knowledge that the
| behaviour won't break or change in the next release of VB?
I would think you would be safe, As I stated I consider the error in .NET
1.x above to be a bug.

Nothing is defined (has always been defined) as the "default value" for a
type.
http://msdn2.microsoft.com/en-us/0x9tb07z(VS.80).aspx

For reference types this means a null reference. For value types it means
"all zeros" or False in the case of Boolean, For structures all its members
are set to their defaults. In the case of DateTime this happens to be
DateTime.MinVal ue.

For example try:

Dim empty As DateTime = Nothing
Dim minValue As DateTime = DateTime.MinVal ue

Debug.Assert(em pty = minValue)
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Oenone" <oe****@nowhere .com> wrote in message
news:ub******** ******@TK2MSFTN GP04.phx.gbl...
| In our applications, we use the special value of DateTime.MinVal ue to
| represent "null dates" throughout all our code. We recently ran into an
| issue where we wanted an optional date parameter for a procedure. We
weren't
| able to declare it with DateTime.MinVal ue as its default value, as
MinValue
| is a read-only property rather than a constant. To work around, we had to
| use a "magic date" that we checked for later on. I was never very happy
with
| that solution.
|
| Today however I noticed that VB2005 is quite happy with the following:
|
| \\\
| Public Sub DoSomething(Opt ional ByVal SomeDate As Date = Nothing)
| [...]
| End Sub
| ///
|
| (I believe this fails to compile in VB2003).
|
| The value that is present in SomeDate if no date is provided by the
calling
| code is DateTime.MinVal ue (i.e., the default value for an uninitialised
date
| variable).
|
| Is this the intended documented behaviour of declaring the optional
| parameter in this way? Can I use this safe in the knowledge that the
| behaviour won't break or change in the next release of VB?
|
| Thanks,
|
| --
|
| (O)enone
|
|
Jun 15 '06 #3

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

Similar topics

5
9491
by: (Pete Cresswell) | last post by:
I've dabbled in "Optional" over the last few days and think I'm coming down against using it. Seems to me like it makes the code harder to read and more complicated. Instead of using Optional, I'm making the parm a Variant and passing Null when it's not used.... then checking for "If Len(theParmValue & "") > 0... to see if it is present....
32
2656
by: Indrid Colt | last post by:
Thank's for your help ! Indrid
13
25287
by: John A Grandy | last post by:
apparently references of type Date can not assume the value Nothing, because the following code fails: Dim d As Date .......other code...... If d Is Nothing Then End If so then what is the equivalent to Nothing for a reference of type Date ?
5
8067
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right thing to do. And I wonder why VB .NET keeps silence and makes such function return some default value instead. Isn't it error-prone? -- Dmitriy...
8
4835
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second,...
2
1491
by: Amanda | last post by:
This is hotel reservation when a departure date is changed manually by user input ( to a valid date in date format - validity is checkedsomewhere else), the arrival date is set to the previous date upon MouseLeave. How do I calculate the date of the previous day (see mdatArrivalDate = ???? )? (I have already done calculating departure...
6
5801
by: Queez | last post by:
I've had a good look around and no-one seems to have mentioned this, which leads me to believe that I may be missing something simple. Basically, is there a way I can do the following, and if so, how?: Sub ResolveArrays(ByVal array1() As String, ByVal valueX As String, _ Optional ByVal array2() As String = {""}, Optional ByVal retry As...
0
1362
by: Peter | last post by:
Hi I'm stuck and would appreciate some help. I have created a Class library project that contains a number of interface definitions that must be used by a range of other VB.NET applications. One of the interfaces has been defined as follows: Public Interface IPlugin
6
2391
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam § Function() § { /...do something.../ someType var § = something; /...do something.../ return something;
0
7459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7393
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7749
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5322
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.