0

I want to write below VB 6 Code to C#

StartDate = '01/06/2015'
EndDate = DateAdd("d", -(Day(DateAdd("m", -(Month(DateAdd("yyyy", 1, StartDate)) - 1), DateAdd("yyyy", 1, StartDate)))), DateAdd("m", -(Month(DateAdd("yyyy", 1, StartDate)) - 1), DateAdd("yyyy", 1, StartDate)))

So i did this C# Code :-

DateTime StartDate = new DateTime(2015, 6, 1);
DateTime dtNo = StartDate.AddYears(1);
DateTime dt1 = StartDate.AddYears(1).AddMonths(-(dtNo.Month - 1));
DateTime dtDayNo = StartDate.AddYears(1).AddMonths(-(dtNo.Month - 1));
EndDate = dt1.AddDays(-dtDayNo.Day);

Just want to know is it correct? I am not getting the way how to test VB 6 Code. So that i can compare the results of c# code with vb code.

Anup
  • 9,396
  • 16
  • 74
  • 138
  • 1
    If you have MS Office, VBA is a dialect of VB6. If you put that into a macro, it should allow you to test the function of the VB6 version. VBScript is also based on VB6, but I'm not entirely sure that the language supports everything. – theB Sep 16 '15 at 09:10
  • 2
    If i were you, I would reference Microsoft.VisualBasic.dll and used `DateAdd` that is defined there (not for testing, for actual code). – GSerg Sep 16 '15 at 09:17
  • @GSerg When using `Microsoft.VisualBasic` can i use the 1st code block in the above question & get the result of `EndDate` variable? – Anup Sep 16 '15 at 09:48
  • @Anup Yes, but you will have to put `DateAndTime.` before each function name because these are static functions in a VB module. Unless you're using C# 6.0 where you can [import static functions](http://stackoverflow.com/q/31852389/11683) with `using`. – GSerg Sep 16 '15 at 09:55
  • You mean if i have to use `DateAdd` then i should use it like `DateTime.DateAdd`? I included the `Microsoft.VisulaBasic.dll` but I cannot find `DateAdd` method. Also i want to use `InStr`, I am not getting how to use this functions? – Anup Sep 16 '15 at 10:05
  • Put `using Microsoft.VisualBasic;` at the top of the module, then use `DateTime.DateAdd` in code. Same for `InStr`, that's `Strings.InStr`. – GSerg Sep 16 '15 at 10:07
  • I already included `using Microsoft.VisualBasic;`.I got `InStr` but not getting `DateTime.DateAdd`. – Anup Sep 16 '15 at 10:12
  • 1
    It's `DateAndTime.DateAdd`. – GSerg Sep 16 '15 at 10:19

1 Answers1

0

I believe this question should be "How do I test VB6 code?"

Luckily, Microsoft has been lazy enough to never update VBA, which is the visual basic 6 variant that comes with microsoft office. Open MS Excel, click the top-left button, and click "options". There should be a checkbox saying something like "show developer tab in ribbon" (I have the dutch version so I don't know the exact text).

Once you've done that, close the options menu and go to the developer tab in the ribbon. Click "Visual Basic" and voila! You've got yourself a simple VB6 IDE that allows you to create forms and integrate them into your excel file.

Peethor
  • 151
  • 5
  • `Microsoft has been lazy enough to never update VBA` - [not exactly true](http://stackoverflow.com/q/3072356/11683). Also you can press Alt+F11 instead of all that. – GSerg Sep 16 '15 at 09:18