Formatting a string 
Author Message
 Formatting a string

I know I'm being dense, so feel free to reprimand me for asking, but...

In VB6 I've got a line of code that formats a phone number as follows:



substitution appears).

I've hunted through the help and have found format specifications for
numbers, dates and times but have been unsuccessful finding information on
how to format a string.

Help?



Mon, 31 Jan 2005 08:50:46 GMT  
 Formatting a string

Quote:

>I know I'm being dense, so feel free to reprimand me for asking, but...

>In VB6 I've got a line of code that formats a phone number as follows:



>substitution appears).

>I've hunted through the help and have found format specifications for
>numbers, dates and times but have been unsuccessful finding
information on
>how to format a string.

Try:

--
Peter Wu
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no
rights.



Mon, 31 Jan 2005 11:20:56 GMT  
 Formatting a string
Try this code:

                Dim s As String
                s = "11231232333"

                MsgBox(s)

Best regards,
Perry Deng
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.



Mon, 31 Jan 2005 11:40:12 GMT  
 Formatting a string

[Reply posted to group and cc: to author as a courtesy (if no 'spam guard')]
Please do not reply directly to the e-mail

Quote:
>>I know I'm being dense, so feel free to reprimand me for asking, but...

>>In VB6 I've got a line of code that formats a phone number as follows:



>>substitution appears).

>>I've hunted through the help and have found format specifications for
>>numbers, dates and times but have been unsuccessful finding
>information on
>>how to format a string.

>Try:


Thanks, I'll give that a shot.

Ultimately though I'd like to not use VB6 compatibility to carry out this
operation. I guess I worded the questions poorly. What I really wanted to know
was the VB.Net way to solve the problem.



Mon, 31 Jan 2005 13:49:38 GMT  
 Formatting a string

Quote:
>Ultimately though I'd like to not use VB6 compatibility to carry out
this
>operation. I guess I worded the questions poorly. What I really wanted
to know
>was the VB.Net way to solve the problem.

Try:

        m_sPhoneNbr = "1664567890"
        m_sPhoneNbr = String.Format("1-{0:###-###-####}",
Convert.ToInt32(m_sPhoneNbr))

HTH.

--
Peter Wu
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no
rights.



Mon, 31 Jan 2005 17:42:40 GMT  
 Formatting a string

Quote:

> [Reply posted to group and cc: to author as a courtesy (if no
> 'spam guard')] Please do not reply directly to the e-mail

> >>I know I'm being dense, so feel free to reprimand me for
> >> asking, but...

> >>In VB6 I've got a line of code that formats a phone number as
> >> follows:



> >> (i.e. no substitution appears).

> >>I've hunted through the help and have found format
> >> specifications for numbers, dates and times but have been
> >> unsuccessful finding
> >information on
> >>how to format a string.

> >Try:

> Thanks, I'll give that a shot.

> Ultimately though I'd like to not use VB6 compatibility to carry
> out this operation. I guess I worded the questions poorly. What
> I really wanted to know was the VB.Net way to solve the problem.

Why do some people talk about "compatibility"? I don't understand this. Why
do people avoid to use the "Microsoft Visual Basic .NET Runtime"? Just
because it carries "MSFT" and/or "VB" in it's name? I'm sure the same people
would call the same library very useful if it came from a 3rd party
distributor and they would even pay for it. I'd decide whether to use it or
not by looking at it's features not by it's name.

Using the assembly, it's still the "VB.net" way. Or you mustn't use the
windows forms libraries as well because it's also used by C# developers.

Armin



Mon, 31 Jan 2005 18:45:34 GMT  
 Formatting a string

Quote:
> > Ultimately though I'd like to not use VB6 compatibility to carry
> > out this operation. I guess I worded the questions poorly. What
> > I really wanted to know was the VB.Net way to solve the problem.
> Why do some people talk about "compatibility"? I don't understand this.
Why
> do people avoid to use the "Microsoft Visual Basic .NET Runtime"?

What I'm avoiding is using the Microsoft.VisualBasic.Compatibility.VB6
namespace. The best reason can be found on MSDN...

"Caution   Functions in the Visual Basic 6.0 Compatibility library are
provided only for use by the upgrading tools. Although it is possible to use
this library when writing new code, there is no guarantee that it will be
supported in future versions of Visual Basic."

Quote:
> Just because it carries "MSFT" and/or "VB" in it's name?

No, because it uses old approaches to solving problems. It is not
object-oriented, the method and data do not live in the same package when
using it. For example:

Microsoft.VisualBasic approach

Mid( myString, 3, 5 )

System approach

myString.Substring( 3, 5 )

I prefer the latter. I think it encourages developers to think in an OO
paradigm, leading to overall better engineered software. If you prefer the
former, knock yourself out and use it. You don't work for me, so I honestly
don't care.



Mon, 31 Jan 2005 23:14:58 GMT  
 Formatting a string


Quote:
> >Ultimately though I'd like to not use VB6 compatibility to carry out
> this
> >operation. I guess I worded the questions poorly. What I really wanted
> to know
> >was the VB.Net way to solve the problem.

> Try:

>         m_sPhoneNbr = "1664567890"
>         m_sPhoneNbr = String.Format("1-{0:###-###-####}",
> Convert.ToInt32(m_sPhoneNbr))

I'm turning into one of those people that I hate, only giving bits and
pieces at a time. Sorry about this, but...

One of the reasons we store the phone number as a string is because some
users want to be able to enter things like "800JOEBLUE." In other words, it
isn't always all digits, but we still want to display it as 1-800-JOE-BLUE.

I'm starting to think I may have to write my own format provider for phone
numbers.



Mon, 31 Jan 2005 23:22:47 GMT  
 Formatting a string

Quote:


> > > Ultimately though I'd like to not use VB6 compatibility to
> > > carry out this operation. I guess I worded the questions
> > > poorly. What I really wanted to know was the VB.Net way to
> > > solve the problem.

> > Why do some people talk about "compatibility"? I don't
> > understand this.
> Why
> > do people avoid to use the "Microsoft Visual Basic .NET
> > Runtime"?

> What I'm avoiding is using the
> Microsoft.VisualBasic.Compatibility.VB6 namespace. The best
> reason can be found on MSDN...

The format function is part of the Strings class contained in the
"Microsoft.VisualBasic" namespace. It's located in
Microsoft.VisualBasic.dll. I'm not talking about
Microsoft.VisualBasic.Compatibility.dll. The library is referenced in every
*new* VB.Net project. MSFT surely wouldn't add it by default if
this...

Quote:
> "Caution   Functions in the Visual Basic 6.0 Compatibility
> library are provided only for use by the upgrading tools.
> Although it is possible to use this library when writing new
> code, there is no guarantee that it will be supported in future
> versions of Visual Basic."

... will apply to the assembly. You only reference the compatibility library
when you're upgrading an application - but I hope for you that's nothing
you're trying.

- Show quoted text -

Quote:
> > Just because it carries "MSFT" and/or "VB" in it's name?

> No, because it uses old approaches to solving problems. It is not
> object-oriented, the method and data do not live in the same
> package when using it. For example:

> Microsoft.VisualBasic approach

> Mid( myString, 3, 5 )

> System approach

> myString.Substring( 3, 5 )

> I prefer the latter. I think it encourages developers to think
> in an OO paradigm, leading to overall better engineered
> software. If you prefer the former, knock yourself out and use
> it. You don't work for me, so I honestly don't care.

You don't work for me either, so you're not forced to use it. ;-)
Just because you don't like the mid function you don't use the whole
library? Thinking more sophisticated, I use Ubound(MyArray) because it's
simpler than MyArray.GetUpperbound(0).

Well, I just didn't understand to call the "Microsoft.VisualBasic" a
compatibility namespace because it is there and it will be there (as long as
you don't delete it...). I probably don't use the most functions too, but
not to use the whole library in general seems to me to be a too hard
decision.

BTW, you shouldn't use VB.Net at all because it still supports the Mid
statement ;-)
Mid(mystring, 5, 1) = "X"

Armin



Tue, 01 Feb 2005 02:52:30 GMT  
 Formatting a string

Quote:
> > What I'm avoiding is using the
> > Microsoft.VisualBasic.Compatibility.VB6 namespace. The best
> > reason can be found on MSDN...

> The format function is part of the Strings class contained in the
> "Microsoft.VisualBasic" namespace. It's located in
> Microsoft.VisualBasic.dll. I'm not talking about
> Microsoft.VisualBasic.Compatibility.dll. The library is referenced in
every
> *new* VB.Net project. MSFT surely wouldn't add it by default if
> this...

If you go back and re-read the thread, the suggested solution to the problem
was to use VB6.Format(), the full name of which would be
Microsoft.VisualBasic.Compatibility.VB6.Format().

That is in the Microsoft.VisualBasic.Compatibility namespace. It's not the
Format function to which you refer, which is Microsoft.VisualBasic.Format().
That version of the function was the one that was not behaving as I wanted
it to. Regardless, it doesn't play in a nice OO paradigm, which was my other
argument.

Quote:
> Just because you don't like the mid function you don't use the whole
> library? Thinking more sophisticated, I use Ubound(MyArray) because it's
> simpler than MyArray.GetUpperbound(0).

I used the Mid function as *one* example to illustrate a concept. All you've
managed to do is further prove my point. The UBound function is not the OO
way to think about things. I would rather see the latter
(MyArray.GetUpperbound(0)) or, better yet, MyArray.Length. Both further
enforce the concept and paradigm of OO. If you'd like I could go on...

I prefer String.IndexOf to InStr.
I prefer String.Replace to Replace.

I could go on, but what it boils down to is that I prefer the idea that an
object has properties and methods, rather than the language has a library of
functions.

Quote:
> Well, I just didn't understand to call the "Microsoft.VisualBasic" a
> compatibility namespace because it is there and it will be there (as long
as
> you don't delete it...). I probably don't use the most functions too, but
> not to use the whole library in general seems to me to be a too hard
> decision.

It's not a hard decision to me at all. It jettisons a lot of legacy {*filter*}
that can be done better (another subjective term of course) in other ways
(i.e. using classes from the System.* namespaces). It also makes it easier
in a shop like ours where we do both VB.Net and CSharp, as we end up using a
consistent approach in both languages.

Quote:
> BTW, you shouldn't use VB.Net at all because it still supports the Mid
> statement ;-)
> Mid(mystring, 5, 1) = "X"

Only if you import Microsoft.VisualBasic or rewrite the line above as
Microsoft.VisualBasic.Mid. That of course brings the discussion full circle,
you're now using functions (not methods) in a legacy library.


Tue, 01 Feb 2005 05:11:17 GMT  
 Formatting a string

Quote:


> > > What I'm avoiding is using the
> > > Microsoft.VisualBasic.Compatibility.VB6 namespace. The best
> > > reason can be found on MSDN...

> > The format function is part of the Strings class contained in
> > the "Microsoft.VisualBasic" namespace. It's located in
> > Microsoft.VisualBasic.dll. I'm not talking about
> > Microsoft.VisualBasic.Compatibility.dll. The library is
> > referenced in every *new* VB.Net project. MSFT surely wouldn't
> > add it by default if this...

> If you go back and re-read the thread, the suggested solution to
> the problem was to use VB6.Format(), the full name of which
> would be Microsoft.VisualBasic.Compatibility.VB6.Format().

> That is in the Microsoft.VisualBasic.Compatibility namespace.
> It's not the Format function to which you refer, which is
> Microsoft.VisualBasic.Format(). That version of the function was
> the one that was not behaving as I wanted it to. Regardless, it
> doesn't play in a nice OO paradigm, which was my other argument.

I thought he used an alias name (Imports VB6 = Microsoft.VisualBasic). But
in this case, you're right and we agree.

Quote:
> [OO]

I can only support your opinion but til now I thought *I* am an OO adict.
;-) I personally don't go so far and I will still use functions like Ubound
or Rnd. I've always used them and they are still there. They can even be
used in other languages. Function like Rnd or Sin are basic functions and
we could probably discuss days whether they are part of a class and about
other OO basics. But we won't... ;-)

Quote:
> > Well, I just didn't understand to call the
> > "Microsoft.VisualBasic" a compatibility namespace because it
> > is there and it will be there (as long as you don't delete
> > it...). I probably don't use the most functions too, but not
> > to use the whole library in general seems to me to be a too
> > hard decision.

> It's not a hard decision to me at all. It jettisons a lot of
> legacy {*filter*}that can be done better (another subjective term of
> course) in other ways (i.e. using classes from the System.*
> namespaces). It also makes it easier in a shop like ours where
> we do both VB.Net and CSharp, as we end up using a consistent
> approach in both languages.

These are facts I didn't know so far. So you've got good reasons for this
decision and that's ok, of course. IMO, as long as I use VB, I use this
library in a few cases. When I use C#, I don't use the VB library. There are
differences anyway. Personal taste and personal decision.

Quote:
> > BTW, you shouldn't use VB.Net at all because it still supports
> > the Mid statement ;-)
> > Mid(mystring, 5, 1) = "X"

> Only if you import Microsoft.VisualBasic or rewrite the line
> above as Microsoft.VisualBasic.Mid. That of course brings the
> discussion full circle, you're now using functions (not methods)
> in a legacy library.

The mid *statement* is not in a library, it's part of the language. Probably
we agree that it's nonsense as strings are unchangable now.

Conclusion: I'll think about it...  ;-)

Armin



Tue, 01 Feb 2005 06:35:04 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Not able to format with String.Format

2. Formatting a string variable in an SQL statement.

3. How To Format a String

4. Formatting a String Variable

5. Trouble Formatting A String

6. how to create formatted numeric string ?

7. format truncating strings

8. Formatting a String

9. format a string in Data Report (VB6)

10. formatting Hex string

11. formatting a string

12. writing 51 as 000051 [Formatting a string for output]

 

 
Powered by phpBB® Forum Software