Distinguished Contributor
404 Posts
3 Kudos
Registered: ‎11-11-2002
Post 1 of 4

Define and Assign variables - VBA vs VB.Net

742 Views, 3 Replies
08-03-2012 08:54 AM

Why do most of the VB.Net examples, define and assign a variable in a single line as it is being used in the program.


In VBA I found it easier to read the code with the variables defined at the top of the function or subroutine.


What do most of you do when defining variables ???


I find the VB.net lines below more difficult to read ..............


For Each acSSObj As SelectedObject In acSSet

Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId,OpenMode.ForWrite)

225 Posts
24 Kudos
Registered: ‎09-15-2011
Post 2 of 4

Re: Define and Assign variables - VBA vs VB.Net

08-03-2012 09:14 AM in reply to: fxcastil

Not only VB.NET, but also all languages in .NET such as C#, we need to declare variables as close to where they used as possible, to keep the scope of those variables tight, then the optimizer will work better.


Especially in AutoCAD .NET API, we need to use Transaction to wrap the code to clear unmanaged-code memory after using, so keep defining and assigning a variable in a single line as much as possible.



*Expert Elite*
5,761 Posts
503 Kudos
Registered: ‎10-12-2006
Post 3 of 4

Re: Define and Assign variables - VBA vs VB.Net

08-03-2012 10:39 AM in reply to: fxcastil

Sometimes its a good idea, sometimes it doesn't matter so much.  For casual code there won't be much difference in optimized code.  If the variable is used over much of the scope then yes, I do put it at the top; if its use is more restricted I tend to do it closer to where its used so less hunting is required.  The important thing is consistency in style so its easy to follow.


Keep in mind those examples are procedural in nature,  not necessarily coding style.  Scattering the relevant pieces to the four coding winds makes the example difficult to follow.


One thing to watch out for is the default value when you declare the variable, especially with objects.  You may end up with a null or other inappropriate value when its used later in the code.  Loops can also be a bit tricky - "new"ing a variable inside each cycle may be inefficient compared to doing it just prior to the loop then reseting or assigning values in each cycle.

If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.

Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 4 of 4

Re: Define and Assign variables - VBA vs VB.Net

08-06-2012 01:21 AM in reply to: fxcastil

You might want to consider declaring variables where they're used. 


My guess about why you see a lot of VB.NET code that does that, is because much of it was originally written in C# and then translated to VB.NET. One key difference between C# and all VB and VBA variants is that variable scope can be explicitly defined in C# (using curly braces), whereas variable scope in VB is the entire body of the sub or function containing the variable, from the point where it is declared to the end of the function/sub. Many code translators will deal with that difference by renaming variables in cases where the same identifier is used in two different scopes that are not nested in each other. While that's perfectly fine in C#, it isn't in VB so code translators try to deal with it by renaming one of the variables.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!