Problem with Format Function

  • Thread starter Thread starter Thunder
  • Start date Start date
T

Thunder

Hi All,
I'm using VB.Net 2005. I have for years used Format to format strings and
dates, etc...
Now when I try to use Format I get the following error?

"'Format' is a type and can not be used as an expression"

Can anyone help?

Thanks,
John.
 
Thunder,

Probably as you show the code that you are using.
This kind of questions are popular.

Cor
 
Thunder said:
I'm using VB.Net 2005. I have for years used Format to format strings and
dates, etc...
Now when I try to use Format I get the following error?

"'Format' is a type and can not be used as an expression"


Unfortunately 'Format' cannot be used to format strings any more.
 
Ok,
then what is to replace it?

I used to be able to write
dim STR as String = Format(Now(), "MM/dd/yyyy")

What would I write now?
 
Hmmm........

VS2005 - VB.NET

Add button to form

Add handler for Click event of button

Add the following to the event handler:

Dim STR As String = Format(Now(), "MM/dd/yyyy")
Console.Writeline(STR)

Press F5

Click the button and magically 12/04/2006 appears in the output window.

How can that happen if 'Format' cannot be used to format strings any more?.
 
Thunder said:
Ok,
then what is to replace it?

I used to be able to write
dim STR as String = Format(Now(), "MM/dd/yyyy")

What would I write now?

Herfried K. Wagner said:
Unfortunately 'Format' cannot be used to format strings any more.

I don't know why you feel you can't use Format (or why you're being told
you can't) but the following works perfectly for me! -

Dim sString As String = Format(Now, "MM/dd/yyyy")
Debug.Print(sString)

Maybe post the code you're having problems with so we can see where you
might be going wrong.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
Ok,
then what is to replace it?

I used to be able to write
dim STR as String = Format(Now(), "MM/dd/yyyy")

What would I write now?

[Please put your reply *after* the text you're quoting.]

DateTime.Today.ToString("yyyy-MM-dd")

Or it may be that you don't have "Imports System.String" to tell it which
bit of namespace to look in for the Format method.

Always put "Option Strict On" at the top so it knows to tell you when it's
unsure of something.

Andrew
 
Thanks,
That is what I was looking for.
The DateTime.Parse(<datevalue>).Tostring("MM/dd/yyyy")
returns the correct value.

I have used format I think since version 4 of VB, I just recently switched
from VS.NET 2003 to 2005 and ran into this situation. So if someone is able
to get Format working in VS.NET 2005, I don't know what my problem is?

Thanks all for you help.

Andrew Morton said:
Ok,
then what is to replace it?

I used to be able to write
dim STR as String = Format(Now(), "MM/dd/yyyy")

What would I write now?

[Please put your reply *after* the text you're quoting.]

DateTime.Today.ToString("yyyy-MM-dd")

Or it may be that you don't have "Imports System.String" to tell it which
bit of namespace to look in for the Format method.

Always put "Option Strict On" at the top so it knows to tell you when it's
unsure of something.

Andrew
 
Stephany said:
Hmmm........

VS2005 - VB.NET

Add button to form

Add handler for Click event of button

Add the following to the event handler:

Dim STR As String = Format(Now(), "MM/dd/yyyy")
Console.Writeline(STR)

Press F5

Click the button and magically 12/04/2006 appears in the output window.

How can that happen if 'Format' cannot be used to format strings any more?.

In addition, you can use String.Format for complex formatting:

Dim str As String = String.Format("The current date is {0:MM/dd/yyyy}",
DateTime.Now)
Console.WriteLine(str)

Chris
 
Back
Top