The three msg boxes illustrate my point. I remember reading somewhere
that "" is sometimes not the same and Nothing in VB, I think that
this might have something to do with it.



The fact that it tells me that (""=Nothing) is true should mean
that if I pass nothing or "" into the function it will produce the
same results.



However when I pass "" in I get an exception and nothing works
fine.



This is taken from code that the vs 2k5 designer has generated so
I'm sure that there must be a way of getting it working. (I get this
error when I data bind an empty text box to a date or decimal value
(works fine with strings) and expect it to covert the empty value to
null)



I've come up with a work around however it's dodgy.

       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

         MsgBox((Nothing = "").ToString) 'Returns "True"
         MsgBox(doshit(Nothing)) 'Returns "not a date"
         MsgBox(doshit(conv(""))) 'WORKS!
   MsgBox(doshit("")) ' THorws exception



     End Sub

     Function doshit(ByVal aDate As System.Nullable(Of Date)) As String

         If aDate.HasValue Then
             Return CType(aDate, Date).ToShortDateString
         Else
             Return "Null Date"
         End If

     End Function



     Function conv(ByVal val As String) As Object

         If val = Nothing Then
             Return Nothing
         Else
             Return val
         End If

     End Function

  You see I tried to create a function that converts a accepts a string
and returns a string, if I return a a string Nothing it seems to revert
back to "", however if I return an object from a function as
Nothing is stays as nothing and the system.nullable works fine.

  I can only guess that this would not be an issue in C#, though I
haven't done much work in it I hear that this main difference
between the too languages is that (Nothing="") returns false in C#.

  The fact though that the designer is generating code like this that
doesn't work makes me think though that there is an easier way that
I've missed.

  Has anyone else run into this problem when using the designer and using
empty text boxes for Date or Decimal Values expecting them to be
converted to null? Am I doing something wrong?

Thanks,

Joel


--- In [email protected], "Joel Dickson" <[EMAIL PROTECTED]> wrote:
>
> Yeha that will work but I'm trying to work with the designer in VS
> 2005. This is code that it's generated (well it's a simplification
> of it anyway).
>
> So there must be someway of making it work.
>
> Thanks,
>
> Joel
>
>
> --- In [email protected], "Ryan Olshan" teranetlists@
> wrote:
> >
> > Instead use DateTime.TryParse.
> >
> > Function dostuff(ByVal aDate As String) As String
> > Dim isDate As Boolean
> > Dim parsedDate As DateTime
> >
> > isDate = DateTime.TryParse(aDate, parsedDate)
> >
> > If (isDate)
> > Return parsedDate.ToShortDateString()
> > Else
> > Return "Not a valid date"
> > End Function
> >
> > HTH,
> > Ryan
> >
> > Thank you,
> > Ryan Olshan
> > Website - http://www.StrongTypes.com
> <http://www.strongtypes.com/>
> > Group - http://groups.yahoo.com/group/StrongTypes
> > Blog - http://blogs.strongcoders.com/blogs/ryan/
> >
> >
> > _____
> >
> > From: [email protected] [mailto:[EMAIL PROTECTED] On
> Behalf Of
> > Joel Dickson
> > Sent: Sunday, April 30, 2006 10:32 PM
> > To: [email protected]
> > Subject: [AspNet2] Issue with Nullable Generic Structure
> >
> >
> >
> > I'm having an issue with passing values from text boxes through to
> > the fill subroutines on my dataTableAdaptors.
> >
> > Basically this is a simple example that illustrates my issue
> >
> >
> >
> > Private Sub Button1_Click(ByVal sender As System.Object,
> ByVal e As
> > System.EventArgs) Handles Button1.Click
> >
> > MsgBox((Nothing = "").ToString) 'Returns "True"
> > MsgBox(dostuff(Nothing)) 'Returns "not a date"
> > MsgBox(dostuff("")) 'Throws Exception - Conversion from
> string
> > "" to type 'Date' is not valid.
> >
> > End Sub
> >
> > Function dostuff(ByVal aDate As System.Nullable(Of Date)) As
> String
> >
> > If aDate.HasValue Then
> > Return CType(aDate, Date).ToShortDateString
> > Else
> > Return "not a date"
> > End If
> >
> > End Function
> >
> > I'm unsure what to do here, I tried to create a function to
> converts
> > empty strings to Nothing and execute that before passing the
> values of
> > the text boxes to the fill functions but that produces the same
> error.
> >
> > Thanks in advance,
> >
> > Joel
> >
> >
> >
> >
> > [Non-text portions of this message have been removed]
> >
> >
> >
> >
> >
> >
> > SPONSORED LINKS
> > Object
> > <http://groups.yahoo.com/gads?
> t=ms&k=Object+oriented&w1=Object+oriented&w2=P
> > rogramming+languages&c=2&s=48&.sig=f6H4QQqcmcp9kaxQRYmjcw> oriented
> > Programming
> > <http://groups.yahoo.com/gads?
> t=ms&k=Programming+languages&w1=Object+oriente
> > d&w2=Programming+languages&c=2&s=48&.sig=r1ya7ggnEBNyfAH5-mYlDw>
> languages
> >
> >
> > _____
> >
> > YAHOO! GROUPS LINKS
> >
> >
> >
> > * Visit your group "AspNet2
> <http://groups.yahoo.com/group/AspNet2> "
> > on the web.
> >
> >
> > * To unsubscribe from this group, send an email to:
> > [EMAIL PROTECTED]
> > <mailto:[EMAIL PROTECTED]>
> >
> >
> > * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> Service
> > <http://docs.yahoo.com/info/terms/> .
> >
> >
> > _____
> >
> >
> >
> >
> > [Non-text portions of this message have been removed]
> >
>




[Non-text portions of this message have been removed]






SPONSORED LINKS
Object oriented Programming languages


YAHOO! GROUPS LINKS




Reply via email to