3

So as an excercise in utility i've taken it upon myself to convert one of our poor old vb .net 1.1 apps to C# .net 4.0.

I used telerik code conversion for a starting point and ended up with ~150 errors (not too bad considering its over 20k of code and rarely can i get it to run without an error using the production source) many of which deal with time/date in vb versus c#.

my question is this how would you represent the following statement in VB

If oStruct.AH_DATE <> #1/1/1900# Then

in C#? The converter gave me

            if (oStruct.AH_DATE != 1/1/1900 12:00:00 AM) {

which is of course not correct but I cannot seem to work out how to make it correct.

sinrtb
  • 136
  • 1
  • 2
  • 6
  • create a new DateTime object and compare against it – Yusubov Sep 27 '12 at 01:53
  • I hope you did not volunteer for the conversion task! – NoChance Sep 27 '12 at 02:16
  • Actually yes, not only is this a volunteer project this is a project I've taken on in my own time. The app is a decade old mess of spaghetti code and has been supported by 'fly by night contractors' and no 'on the clock time has been given to updating the code base or even cleaning it. Of 20k lines of code at least 3-5 is commented out code changes – sinrtb Sep 27 '12 at 20:50

3 Answers3

3
 if (oStruct.AH_DATE != new DateTime(1900, 1, 1){
Dan J
  • 16,319
  • 7
  • 50
  • 82
Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
2

You may try this construct :

if ( DateTime.Compare(oStruct.AH_DATE, new DateTime(1900, 1, 1)) == 0 )
{
// your code here
}

Here is a reference how to compare DateTimes in .NET - DateTime.Compare Method

Note: Comparing two dates like integers may lead you to wrong results. Because, date-time structure in .NET has its specific properties. I would always try to be attentive when comparing two dates, and always choose DateTime.Compare to be on a safer side.

Dan J
  • 16,319
  • 7
  • 50
  • 82
Yusubov
  • 5,815
  • 9
  • 32
  • 69
1

This should work:

if (oStruct.AH_DATE != DateTime.Parse("1/1/1900"))
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • It works, but you may as well use the `DateTime` constructor, if the value you're passing in is a constant literal anyway. – Dan J Sep 27 '12 at 16:05
  • DateTime does not have a constructor taking a string literal. – Dave Doknjas Sep 27 '12 at 16:10
  • Indeed. But it does have constructors that take integers and, since you're *providing* the string literal in the first place, you could just as easily provide those, thus avoiding a method call... or the possibility of a runtime error due to a typo in the string. :P – Dan J Sep 27 '12 at 20:25
  • Yes - of course. I'm always approaching these things from an automatic conversion point of view (since that's what I do). – Dave Doknjas Sep 27 '12 at 20:29