§ ¶Ugly variable definition syntax
In some programming languages, you must declare a variable by use, with a specific type:
ItemList count;
In other languages, you can declare a variable which can hold any type:
var item = document.getElementById('myForm');
In yet other languages, variables can be implicitly declared on use:
logOutput = DoCommand("somestring")
And then... there's Visual Basic.
Dim output As OutputWindow = env.GetOutputWindow()
Yuuuuuck! How do people stand this??
I don't ordinarily write Visual Basic code, but dug in a little bit in order to write a macro to provide the missing BuildStartupProject command that VS2005 still lacks. I'm not opposed to BASIC, but this has got to be the ugliest syntax hack ever, considering that DIM stands for "dimension" (it was originally used to declare sized arrays).
Comments
Comments posted:
My favourite are some graphics commands which were apparently inherited back from GW-BASIC-days to modern time VB. I googled this for drawing a line for instance: "picture.line (i+200,500)-(i+400,3000), vbBlue, BF". Oh yeah, that draws a box (BF parameter). Same thing with circles. When you're used to see
obj.method(arg1, arg2, arg3) you suddenly get
obj.method(arg1, arg2), arg3 !!
But maybe that's just me.
(sorry if double-post, but your preview seems to have forgotten to pass the data through :-p)
eloj - 12 06 06 - 10:02
Actually, this is configurable. You can set VB to define variables on initialisation. Check out:
http://msdn2.microsoft.com/en-us/library..).aspx
Steven - 12 06 06 - 10:43
VB's variable declaring is even uglier:
Dim a,b,c as Integer
What was declared here? (and if you answer 3 integers, the answer is wrong)
ender - 12 06 06 - 17:39
Yeah, that was the case up to VB6. However in VB.NET all of them will be Int32s.
But nevermind, there's plenty of reasons left why VB's still a pain in the ass.
Gregorius - 12 06 06 - 19:58
It’s just syntax; once you are used to it it’s not that bad.
I especially like how you can do:
Dim fs As New System.CodeDom.CodeArgumentReferenceExpression()
instead of (C#):
System.CodeDom.CodeArgumentReferenceExpression fs = new System.CodeDom.CodeArgumentReferenceExpression();
Paul - 12 06 06 - 23:52
Yeah, I've definitely run into that problem a bunch of times -- it occurs in C++ too if you can't allocate on the stack. I believe that a solution is planned for both C++ and C# with type-inferred declarations, with "var fs" for C# 3.0 and "auto fs" for C++0x.
Phaeron - 12 06 06 - 23:59
On the other hand, the VB.net array syntax is just nasty...
How many elements does the following array have?
Dim a As Integer() = New Integer(500) {}
Answer: 501.
Oh well, at least they got rid of one-based arrays (thank god).
Paul - 13 06 06 - 00:21
instead of (C#):
System.CodeDom.CodeArgumentReferenceExpression fs = new System.CodeDom.CodeArgumentReferenceExpression();
That isn't that bad in Visual Studio. as soon as you hit the space after the 'w' in new, it will bring up IntelliSense and you just have to press '(' and everything in between it written for you.
What happens in:
Dim a,b,c as Integer
?
Bryan - 13 06 06 - 00:38
In "Dim a, b, c as Integer", "c" is an Integer and "a" and "b" are two Variants (variables which can hold any type). If you want declare 3 integers you must write "Dim a as Integer, b as Integer, c as Integer".
I use VB6 (not .NET) for 2/3 years and I don't think it's "ugly".
I've made many useful functionally programs without lots of problems.
I'm studying C/C++ in school and I understand that C is more "low level" than VB, but if you're developing a simple application which don't need low level istructions (obviously it can't be something like VirualDub) VB is perfect for it.
Sorry for my english, i'm italian.
LorenzoS - 13 06 06 - 05:03
When declaring variables in VB, you can save space (but increase hard-to-readability) by using the type character "%". The declaration would be:
Dim a%, b%, c%
Other type characters are & (Long), $ (String), # (Double), ! (Single) etc.
Johan - 13 06 06 - 05:50
Comment form
Please keep comments on-topic for this entry. If you have unrelated comments about VirtualDub, the forum is a better place to post them.