Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi there!

 

Just playing around and got this one on my mind:

 

Which one wold one use:

 

        myString = firstString & ControlChars.Tab & secondString

or

        myString = firstString & vbTab & secondString

 

 

and why...

  • Leaders
Posted

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:

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.

[sIGPIC]e[/sIGPIC]
Posted

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!

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...