joe_pool_is
01-15-2008, 08:09 AM
Can strings be formatted for output in VB6? If so, what is the method called?
For Example, I'd like to be able to do something like this:strFormat = "Reading %d bytes from %d total bytes"Then, in my TextBox, I can simply write:TextBox.Text = Format(strFormat, intCurByte, intTotalBytes)Thanks for the help!
the master
01-15-2008, 08:15 AM
I thing you want replace() instead of format()
joe_pool_is
01-15-2008, 08:33 AM
I thing you want replace() instead of format()Is that the best VB6 has?
I've also got a lot of SQL code. It would be nice to be able to insert parameters into the strings as I need them, so that I can search different fields quickly instead of cluttering up my .EXE with a lot of SQL text.
Seems like replace() would be a lot of overhead and coding for something like that.
Seems like QBASIC had a "PRINT USING" command, but I can't seem to find anything similar in VB6.
the master
01-15-2008, 09:17 AM
You can add strings together instead if you want
dim temp as string
temp = "this is a string " & otherstring & " some more text"
Other than that i think replace() is about all there is
DougT
01-15-2008, 09:43 AM
TextBox.Text = "Reading " & CStr(intCurByte) & " From " & CStr(intTotalBytes)
I found this VB implementation of sprintf that should do what you require:
http://www.developerfusion.co.uk/show/668/
joe_pool_is
01-15-2008, 11:18 AM
I found this VB implementation of sprintf that should do what you require:
http://www.developerfusion.co.uk/show/668/That's what I was looking for. Thanks!
So, VB6 doesn't have this feature built into it, then?
dilettante
01-15-2008, 01:36 PM
As far as SQL goes, you probably should consider parameterized queries anyway.
For formatting variables into String values you might consider the powerful Format$() function:
Option Explicit
'Text1 is Multiline = True.
Private Sub Form_Click()
Dim intCurByte As Integer, intTotalBytes As Integer
intCurByte = 1700
intTotalBytes = 32000
'Example 1: escaped literals.
Text1.Text = Text1.Text & vbNewLine _
& Format$(intCurByte, _
"Re\a\di\ng 0 b\y\te\s fro\m ") _
& Format$(intTotalBytes, _
"0 \to\t\al b\y\te\s")
'Example 2: quoted literals.
Text1.Text = Text1.Text & vbNewLine _
& Format$(intCurByte, _
"""Reading ""0"" bytes from """) _
& Format$(intTotalBytes, _
"0"" total bytes""")
'Example 3: fancy formatting.
Text1.Text = Text1.Text & vbNewLine _
& Format$(intCurByte, _
"""Reading ""##,##0"" bytes from """) _
& Format$(intTotalBytes, _
"##,##0"" total bytes""")
'Example 4: MultiFmt routine with predefined formatting strings.
Dim varFormats() As Variant
varFormats = Array("""Reading ""##,##0"" bytes from """, _
"##,##0"" total bytes""")
Text1.Text = Text1.Text & vbNewLine _
& MultiFmt(Array(intCurByte, intTotalBytes), varFormats)
End Sub
Private Function MultiFmt(Values, Formats) As String
Dim V As Integer
If UBound(Values) <> UBound(Formats) Then
Err.Raise 5, "MultiFmt", "Values and Formats must match"
Else
For V = 0 To UBound(Values)
MultiFmt = MultiFmt & Format$(Values(V), Formats(V))
Next V
End If
End Function
Since it has numerous active formatting symbols, literals must either be escaped where individual symbols might be interpreted as formatting controls or else quoted. Both approaches are shown above.
The results of this sample program look like:
Reading 1700 bytes from 32000 total bytes
Reading 1700 bytes from 32000 total bytes
Reading 1,700 bytes from 32,000 total bytes
Reading 1,700 bytes from 32,000 total bytes
joe_pool_is
01-18-2008, 05:19 AM
Ah! I found it!Dim strFormat As String = "Reading {0} bytes from {1} total bytes"
' in my loop, I can use it like this:
lblDisplay.Text = String.Format(strFormat, intCurByte, intTotalBytes)
Flyguy
01-18-2008, 06:05 AM
What is wrong with the VB6 way of building strings?
I know it's not the C style syntax, but not more difficult or more unreadable, is it?
Based on your code segment you are using VB.Net and not VB6!
joe_pool_is
01-18-2008, 11:25 AM
What is wrong with the VB6 way of building strings?
I know it's not the C style syntax, but not more difficult or more unreadable, is it?The VB6 way? Which one, DougT's? I just want to reduce the open text in our executables, and we use some of these strings often with slight variations.
Based on your code segment you are using VB.Net and not VB6!Sorry about that. My mistake. We use both here. VB6 when patching old stuff; VB.Net and C# for the new stuff. I probably just got sloppy.