Which one is most appropriate to use

Ales Zigon

Newcomer
Joined
Jul 6, 2003
Messages
4
Hi there!

Just playing around and got this one on my mind:

Which one wold one use:

Code:
        myString = firstString & ControlChars.Tab & secondString
or
Code:
        myString = firstString & vbTab & secondString


and why...
 
It's entirely a matter of taste. ControlChars.Tab and vbTab are both constants. ControlChars.Tab is a Char constant equal to a tab character. vbTab is a string constant that contains a single tab character.

When you compile the program, references to constants are replaced with the constant value. Also, when you use a character with the & operator it is automatically converted to a string. In this case, the conversion probably happens at compile time. So these three lines of code should produce the same exact compiled code:
Code:
Dim s As String = ControlChars.Tab
Dim s As String = vbTab
Dim s As String = ChrW(9)
I would choose the second option because it is easy to read and involves less typing, but that's just like uh, my opinion man.
 
That was my idea too. You see, my son is taking some programming course and he was arguing with his teacher about this (teachers idea was to strictly use ControlChars.Tab, so on the exam, my boy was undergraded for using vbTab - silly!)

Thanks!
 
There is a reason why one writes in VB. And it is because it is an elegant language that has many features OVER the *base* framework. The only way that student should have been deducted points is if the test specifically said "*using framework-only features*".... that is anything NOT in the Microsoft.VisualBasic namespace.

For instance in VB you would have to rewrite
If Left(str, 3) = "bla" Then...

with the "framework" only version:

If str IsNot Nothing Then
If str.StartsWith("bla") Then...

The teacher sounds like a C# person. :)

[EDIT].
PS. My point is that vbTab is absolutely the RIGHT thing to do in VB. As is VbCrLf etc. These constants exist for a reason. It is what makes VB powerful.

Disclaimer:
I have been coding C# for 4 years. I coded in VB for 7 years before that. I *love* C#.... but I also hate when VB is disparaged ignorantly by know-nothing people.
 
Back
Top