Format$ question

  • Thread starter Thread starter phhonl
  • Start date Start date
P

phhonl

Dim x As String
x = Format$("50", "00000000")

in vb6 x returns "00000050"

in vb.net 2005 x returns "0000000"

How do I get the vb6 result in vb.net 2005?
 
Dim x As String
x = Format$("50", "00000000")

in vb6 x returns "00000050"

in vb.net 2005 x returns "0000000"

How do I get the vb6 result in vb.net 2005?

How are you doing it in .NET, with the Format function or the
string.Format method?
 
phhonl said:
Dim x As String
x = Format$("50", "00000000")

in vb6 x returns "00000050"

in vb.net 2005 x returns "0000000"

How do I get the vb6 result in vb.net 2005?


Right, it should work like in VB6. Though, this is probably because the
behavior is not documented this way. Valid placeholders when formatting
strings are @, &, <, > and !.

I'd use

x = "50".PadLeft(8, "0"c)


Armin
 
In .net I tried:

x = System.String.Format("50", "00000000")

and that returns "50". I need it to return "00000050"
 
x = "50".PadLeft(8, "0"c)

Thanks, that works!

Armin Zingler said:
Right, it should work like in VB6. Though, this is probably because the
behavior is not documented this way. Valid placeholders when formatting
strings are @, &, <, > and !.

I'd use

x = "50".PadLeft(8, "0"c)


Armin
 
phhonl said:
In .net I tried:

x = System.String.Format("50", "00000000")

and that returns "50". I need it to return "00000050"

The Format.String method doesn't work like the Format$ function. The
format comes first, and the format string has to contain format
specifiers as it can format several values:

x = String.Format("{0:00000000}", 50)

You can use the ToString method to format a single value:

x = 50.ToString("00000000")
 
phhonl said:
Dim x As String
x = Format$("50", "00000000")
in vb6 x returns "00000050"
in vb.net 2005 x returns "0000000"
How do I get the vb6 result in vb.net 2005?

Use the correct Data Types.

"50" is a String and can only be formatted using the /string/ formatting
characters ("@" being the only one, IIRC).

50 is an Integer and can use all the /numeric/ formatting characters, as
you would expect.

x = Format(50, "00000000")

or, better still,

x = 50.ToString("00000000")

VB "Proper" used its Evil Type Coercion to translate the String value
into an integer value for you.

Visual Basic - the shiny new, all grown-up, object-oriented language -
/doesn't/.

HTH,
Phill W.
 
Back
Top