Ales Zigon Posted May 18, 2012 Posted May 18, 2012 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... Quote
Leaders snarfblam Posted May 18, 2012 Leaders Posted May 18, 2012 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. Quote [sIGPIC]e[/sIGPIC]
Ales Zigon Posted May 19, 2012 Author Posted May 19, 2012 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! Quote
cmoya Posted May 22, 2012 Posted May 22, 2012 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.