|
-
Oct 4th, 2006, 07:10 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2005] Best way to write tabs
There seems to be quite a few ways to add a tab to a text file
VB Code:
ControlChars.Tab
Convert.ToChar(Keys.Tab)
vbTab
\t
ChrW(20)
Have I missed any?
Is there a recommended preference of one over another?
The first one seems most succinct and .NET-like
-
Oct 4th, 2006, 08:00 PM
#2
Re: [2005] Best way to write tabs
"\t" doesn't exist in VB. That's a C escape sequence so only exists in C-based languages.
In my opinion ChrW(20) or Chr(20) is a stupid way to do it. Like so many Runtime functions ChrW and Chr have somewhat cryptic names that would mean nothing to the uninitiated. The number 20 is even more meaningless unless you know your ASCII table pretty well. Also, you're invoking a method so there's no gain in efficiency. Use of Runtime functions is undesirable to many also.
vbTab is a VB6 constant that still exists in VB.NET but the Microsoft recommendation is to replace its use with ControlChars.Tab. Having said that, ControlChars is a member of the Microsoft.VisualBasic namespace so is also undesirable to some.
If you don't have an issue with using the Microsoft.VisualBasic namespace then take Microsoft's recommendation and use ControlChars.Tab. If you prefer to avoid that namespace then use Convert.ToChar(Keys.Tab). Both are self-documenting, unlike Chr(20) and ChrW(20). Avoid those like the plague. Note that ControlChars.Tab is a constant so is more efficient than calling the Convert.ToChar method, although in practice the difference would be so miniscule as to be meaningless.
-
Oct 4th, 2006, 08:06 PM
#3
Re: [2005] Best way to write tabs
making your own constant equal to chrW(9) is the most ".net like" in my opinion.
ControlChars.Tab
vbTab
are both part of the visualbasic namespace, which while it is part of the .NET framework, its only used in VisualBasic, and wouldn't be found in another .NET language app. Some programmers remove this reference to avoid using it.
Convert.ToChar(Keys.Tab)
is just converting 9 to a char, which is a tab (same as ChrW(9))
ChrW(20) <--- not sure what that is.. its not a tab though?
\t <-- not sure what this is either [edit]John cleared that up above [/edit]
At the end of the day though, using any of the ones that work will get the job done just as well as the other ones... even if there is a millisecond of difference, the user won't see it.
I also like the whole public function approach passing an optional param of how many tabs you want. I use the same type of function with Environment.NewLine to easily return multiple line spaces...
VB Code:
Public Shared Function Tab(Optional ByVal Count As Integer = 1) As String
Dim temp As String
For i As Integer = 1 To Count
temp &= ChrW(9)
Next
Return temp
End Function
-
Oct 4th, 2006, 08:11 PM
#4
Re: [2005] Best way to write tabs
There you go. I didn't know that 20 wasn't the ASCII code for Tab or that 9 was. Validation of my point about using ASCII codes when there are self-documenting alternatives available. The only ASCII codes I know by heart are 13 and 10. There's rarely a need to know them these days but I know where to find them if I do need them. The use of Keys.Tab is preferable in my book because, while it is simply an alias for a number, it tells you exactly what it is without commenting or a need to search.
-
Oct 5th, 2006, 12:02 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Best way to write tabs
It doesn't surprise me that I got the ASCII wrong either, I was typing it from memory.
and the \t must have slipped in from my C++ class 
I quite like the public function idea though.
I hadn't thought of that before
-
Oct 5th, 2006, 12:19 AM
#6
Re: [RESOLVED] [2005] Best way to write tabs
You don't really need that method though. If you wanted a string with, say, 10 tabs you could just do this:
VB Code:
Dim myString As New String(Convert.ToChar(Keys.Tab), 10)
-
Oct 5th, 2006, 09:14 AM
#7
Re: [RESOLVED] [2005] Best way to write tabs
 Originally Posted by jmcilhinney
You don't really need that method though. If you wanted a string with, say, 10 tabs you could just do this:
VB Code:
Dim myString As New String(Convert.ToChar(Keys.Tab), 10)
Well I primarily use a public method for NewLine, since its a string itself, you can't use it to create a string with a specified count. I suppose that would work for tabs though. I generally like the public method because it makes it nice and simple to grab newlines when you need them. I guess that happens less frequently with tabs.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|