Question DateTime as Optional Parameter in Constructor

jmfrank63

Member
Joined
Mar 5, 2013
Messages
5
Programming Experience
1-3
Hello Community,

this is my first time and my first question, so if I do something wrong please notify me.

I am trying to build a class with a date as optional parameter. If none is given the current date shall be assumed.

Since optional parameters require a default value I tried the following:


...
Public Sub New(Optional ByVal givendate As System.DateTime = System.DateTime.MinValue)
...


Unfortunately this does not work. The error is : "Constant value required". Has anyone an idea how achieve my goal to have an optional date as parameter?

Thank you for your audience.

Johannes Frank
 
You would be better off overloading rather than using optional parameters. If however, you have your heart set on it then make the optional parameter of type object and default value nothing. You can then cast it to DateTime and check if nothing.

Really though, overload.
 
Hi,

To aid you with Lotok's answer regarding Overloading your class. This means to add multiple Definitions of declaring a variable of your Class Type using the New Keyword. As an example, have a look at this:-

VB.NET:
Public Class MyClassExample
  Public Property DateToUseInClass As DateTime
 
  Public Sub New()
    DateToUseInClass = System.DateTime.Now
  End Sub
 
  Public Sub New(ByVal GivenDate As System.DateTime)
    DateToUseInClass = GivenDate
  End Sub
End Class

This means that you can declare a variable of this type in two ways.:-

1) If you did NOT know the date to be assigned to the class you would just call:-
VB.NET:
Dim myVariable As New MyClassExample
Which then assigns the current DateTime to a property in your class.

or

2) If you DID know the date to be assigned to the class you would call:-
VB.NET:
Dim myVariable As New MyClassExample(DateTime.Today.AddDays(-1))
Which, in this case, then assigns the current DateTime, minus 1 day, to a property in your class. This can then be any valid date of your choosing.

Hope that helps.

Cheers,

Ian

BTW, I agree with Lotok on how this should be done since this is the correct way to do this.
 
Last edited:
Thank you Ian and Lotok, I will try overloading at once. I didn't come up with this yet, but I hope the class is still XML serializable. I'll inform you on the results.
Nevertheless this forum seems to be just the right place for my questions. I asked the same question yesterday on stackoverflow and got no response up till now.
One more stupid question though, I couldn't find it: How do I mark code? It's not obvious in the icons on top.
Thanks again.
 
Everything looks fine, I was concerned, for serialization is a little picky about what types to support. For instance collection class is not supported... but overloading seems to be no problem.
How to mark the question as solved?

Thanks again.
 
Yes, but I must admit I don't like binaries except for the executable. Hinders people from looking into things. I use ArrayList instead and it suits just fine.
 
Thanks for the advice. I immediately asked Google for the reason and it came up with the following from Stackoverflow:

ArrayLists are essentially deprecated as they're untyped - you need to use casts with them - and they're slower and less space efficient for value types because they require the items to be boxed. ...


So I did what you proposed.

It's not easy to return to a static typed language when you got used to the freedom python gives you. But I like the .NET Framework. Make things really easy.
 
Back
Top