convert Date type property to String

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

how are people dealing with the situation where a function accepts a String
representation of a date ... but a control on the page or form returns a
Date value ...

strangely, these Date values can be Nothing but not in the sense of a object
reference to Nothing ...

rather, you supposedly can use an equality comparison

MyControl.Date = Nothing
 
to convert DateTime to a string

Dim myStringDateTime As String = MyDateTime.ToString()


Regards - OHM


how are people dealing with the situation where a function accepts a
String representation of a date ... but a control on the page or form
returns a Date value ...

strangely, these Date values can be Nothing but not in the sense of a
object reference to Nothing ...

rather, you supposedly can use an equality comparison

MyControl.Date = Nothing

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
* "John A Grandy said:
how are people dealing with the situation where a function accepts a String
representation of a date ... but a control on the page or form returns a
Date value ...

Have a look at the 'DateTime''s 'ToString' method, which can be used to
convert the date to a string.
strangely, these Date values can be Nothing but not in the sense of a object
reference to Nothing ...

'DateTime' is a value type, assigning 'Nothing' will assign the "default
value" of the datatype for value types.
 
Hi John,

I have tried to make this morning an example about this subject, I am not
sure if all is right, so I ask OHM, Armin, HKW or Jay B if they will check
it?

Cor

\\\
Public Module mymodule
Public Sub main()
Dim a As Date = Nothing
If a = Nothing Then
MessageBox.Show("Empty date value")
End If
Dim b As String
If b Is Nothing Then
MessageBox.Show("String with no pointer")
End If
b = ""
If b = Nothing Then
MessageBox.Show("String with 0 characters")
End If
Dim c As mydate
If c Is Nothing Then
MessageBox.Show("Object with no reference")
End If
c = New mydate("31/31/03") 'false to create error
If c.date Is Nothing Then
MessageBox.Show("Object with no property date")
End If
c = New mydate("")
If Not c Is Nothing Then
If c.date Is Nothing Then
MessageBox.Show("Existing object with an empty property
date")
End If
End If
Dim d As Object
d = New Date
If d Is Nothing Then
MessageBox.Show("Empty object date")
End If
d = New Date(Nothing)
If DirectCast(d, Date) = Nothing Then
MessageBox.Show("Empty boxed date")
End If
End Sub
End Module


Public Class mydate
Private mydate As Date
Public Sub New(ByVal dat As String)
If dat <> "" Then
Try
mydate = Convert.ToDateTime(dat)
Catch
End Try
Else
mydate = Nothing
End If
End Sub
Public ReadOnly Property [date]() As mydate
Get
mydate = mydate
End Get
End Property
End Class

///
 
Hi Cor,
I have tried to make this morning an example about this subject, I am
not sure if all is right, so I ask OHM, Armin, HKW or Jay B if they
will check it?

As you've asked for: :-)
I *never* use Nothing with value types. To me, Nothing means "no reference".
I am not really sure what your code aims at. If I understand John correctly,
he wants to convert between Date and String. If the thing is to set a Date
to Nothing or a Date value, I'd write a WrappedDate class (see below).
Public Class mydate
Private mydate As Date
Public Sub New(ByVal dat As String)
If dat <> "" Then
Try
mydate = Convert.ToDateTime(dat)
Catch
End Try
Else
mydate = Nothing
End If
End Sub
Public ReadOnly Property [date]() As mydate
Get
mydate = mydate

Tiny correction:

[date] = mydate
or
return mydate
End Get
End Property
End Class

class WrappedDate
public Value as date
public sub new(byval Value as date)
me.value = value
end sub
end class

Just my 2 Euro-c.
 
Hi Armin,

My aim is to have a sample ready for all those "nothing" situations.
return mydate

I am in deep shame

I always mix the get and the set and have to check it, maybe is the reason
that it goes wrong (I have used it so much but still the same problem).

Now I could change the sampel back to what I wanted, when you have additions
please tell it.
I put the sample as corrected beneath the other, so the others see it also.

Thanx

Cor
 
Corrected while there was a stupidity in that Armin saw.

\\\
Public Module mymodule
Public Sub main()
Dim a As Date = Nothing
If a = Nothing Then
MessageBox.Show("Empty date value")
End If
Dim b As String
If b Is Nothing Then
MessageBox.Show("String with no pointer")
End If
b = ""
If b = Nothing Then
MessageBox.Show("String with 0 characters")
End If
Dim c As mydate
If c Is Nothing Then
MessageBox.Show("Object with no reference")
End If
c = New mydate("")
If c.date = Nothing Then
MessageBox.Show("Existing object with an empty property date")
End If
Dim d As Object
If d Is Nothing Then
MessageBox.Show("Not Existing object")
End If
d = New Date
If Not d Is Nothing Then
MessageBox.Show("Existing object date")
End If
d = New Date(Nothing)
If DirectCast(d, Date) = Nothing Then
MessageBox.Show("Empty boxed date")
End If
End Sub
End Module
Public Class mydate
Private mydat As Date
Public Sub New(ByVal dat As String)
Try
mydat = Convert.ToDateTime(dat)
Catch
mydat = Nothing
End Try
End Sub
Public ReadOnly Property [date]() As Date
Get
Return mydat
End Get
End Property
End Class
///
 
Back
Top