Results differ between Windows App and Console App. Why???

H

Hexman

Hello All,

Writing my first console app I came across a difference in results. I want the "CurrMonthYear" (string) to appear as "06/2006". In the Windows App
thie following code works (it is placed in a textbox). In the Console App using the same code (except being placed in a string variable) the result
is "6/2006" (no leading zero). It appears that the Format functionworks differently between Windows and Console Apps. I got it to work by using a
format string in the ToString method. Just would like to know why.

Any ideas or reasoning? When to use Format over ToString or vice versa?

Thanks,

Hexman

Code working in Windows App:
Dim iMonth As Integer
Dim iYear As Integer
iMonth = Month(Now)
iYear = Year(Now)
tbCurrMonthYear.Text = Format(iMonth, "00") & "/" & iYear.ToString

Code NOT working in Windows App:
Dim iMonth As Integer
Dim iYear As Integer
Dim CurrMonthYear as String
iMonth = Month(Now)
iYear = Year(Now)
CurrMonthYear = Format(iMonth, "00") & "/" & iYear.ToString

Code rewritten to produce same result in Console App:
Dim iMonth As Integer
Dim iYear As Integer
Dim CurrMonthYear as String
iMonth = Month(Now)
iYear = Year(Now)
CurrMonthYear = iMonth.ToString("00") & "/" & iYear.ToString
 
L

Larry Lard

Hexman said:
Oops! The second block of code is "NOT working in Console App".

So this is the part that's not working in the console app?

When I do this I get "06/2006" in CurrMonthYear. How are you displaying
CurrMonthYear?

I get the same thing as with Format.
 
C

Chris Dunaway

Hexman said:
Code working in Windows App:
Dim iMonth As Integer
Dim iYear As Integer
iMonth = Month(Now)
iYear = Year(Now)
tbCurrMonthYear.Text = Format(iMonth, "00") & "/" & iYear.ToString

I can't answer as to the apparent discrepancy between the windows app
and the console. But do you need the iMonth and iYear variables? Why
don't you just use:

tbCurrMonthYear.Text = DateTime.Now.ToString("MM/yyyy")

?


Chris
 
H

Hexman

Answers in text.

So this is the part that's not working in the console app? yes, below.


When I do this I get "06/2006" in CurrMonthYear. How are you displaying
CurrMonthYear?
I have used debug.print and msgbox(CurrMonthYear).
I get the same thing as with Format.
I don't. Maybe I was halucinating. I'll double check.
 
H

Hexman

I can't answer as to the apparent discrepancy between the windows app
and the console. But do you need the iMonth and iYear variables? Why
don't you just use:

tbCurrMonthYear.Text = DateTime.Now.ToString("MM/yyyy")

?

Because I calculate a series of dates (both forward and back). eg;
04/2006, 05/2006, 06/2006, 07/2006, 08/2006, etc.

Now that I think about it I could do something *like*:

tbNextMonthYear = (DateTimeNow + 1 month).ToString("MM/yyyy"), that is as soon as I figure out how to add/subtract ONE MONTH to the date.

Thanks for stimulating thought,

Hexman
 
C

Chris Dunaway

Because I calculate a series of dates (both forward and back). eg;
04/2006, 05/2006, 06/2006, 07/2006, 08/2006, etc.

Now that I think about it I could do something *like*:

tbNextMonthYear = (DateTimeNow + 1 month).ToString("MM/yyyy"), that is as soon as I figure out how to add/subtract ONE MONTH to the date.

Dim dtNextMonth As DateTime = DateTime.Now.AddMonths(1)
Dim dtLastMonth As DateTime = DateTime.Now.AddMonths(-1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top