locked
CDate conversion in VB6 to C#

    Question

  • I have been tasked with converting an old VB6 program to to C#. One function I have been having trouble porting over is the calculation of a birthdate from a filed that was read from a binary file.
    .BirthDate = CDate
    ((CLng
    (recPatient.birthDateByte2) * 256) + (recPatient.birthDateByte1 + 366))
    The only function I could find remotely similar is
    DateTime BirthDate = DateTime.ToDateTime((long
    )recPatient.birthDateByte2) * 256) + (recPatient.birthDateByte1 + 366));
    However ToDateTime(long) just returns InvalidCastException .
    Now I can build the string manually but I can not find any documentation anywhere on VB6's CDate(long). Any help on solving this issue would be greatly appreciated.
    Friday, December 18, 2009 5:59 PM

Answers

  • i found that DateTime.FromOADate(double) behaves the same as CDate.
    DateTime.FromOADate(patientList[i].birthDateByte1 + patientList[i].birthDateByte2 * 256 + 366)
    Tuesday, December 22, 2009 3:44 PM

All replies

  • System.Convert.ToDateTime


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Saturday, December 19, 2009 12:07 AM
  • Hi Scott,

    Try one of the following.>>

            Dim BirthDate1 As Date = Date.FromBinary(((recPatient.birthDateByte2) * 256) + (recPatient.birthDateByte1 + 366))
            'or
            Dim BirthDate2 As Date = Date.FromFileTime(((recPatient.birthDateByte2) * 256) + (recPatient.birthDateByte1 + 366))
            'or
            Dim BirthDate3 As Date = Date.FromFileTimeUtc(((recPatient.birthDateByte2) * 256) + (recPatient.birthDateByte1 + 366))

    The DateFromBinary, Date.FromFileTime and Date.FromFileTimeUtc all convert
     a 64 bit signed or LONG or INT64 value to a valid VB.Net DATE format value.


    Regards,

    John

    I'm currently looking for work in Vb.Net software development. :-)
    Sunday, December 20, 2009 10:05 PM
  • I think you can better ask this in a C# forum, but as this is in Net exactly the same for Visual Basic as for C# (beside that VB uses less characters)


    Simpy construct the datetime new, that is almost for every culture setting the best in Net

    BirthDate = New DateTime(year,month,day) ' the fields are integers

    For the rest, Visual Studio 2008 has in the VB version a build in VB6 language to VB9 language converter so that you can use as well.
    (Be aware that the VB6 compatible namespace will not be in Framework 4 anymore).

     


    Success
    Cor
    Tuesday, December 22, 2009 7:31 AM
  • i found that DateTime.FromOADate(double) behaves the same as CDate.
    DateTime.FromOADate(patientList[i].birthDateByte1 + patientList[i].birthDateByte2 * 256 + 366)
    Tuesday, December 22, 2009 3:44 PM