If you know Visual Basic for Applications and now want to start writing Visual Basic, this article lists four differences encountered in the upgrade process.
Visual Basic is different variables
In VBA you declare a variable with a Dim For example:
MyName As String DIM
MyNumber As Integer DIM
You then assign values to variables in the separate financial statements. Toexample:
MyName = "Bob"
MyNumber = 1
Typically, you declare your variables at the top of a subroutine. In Visual Basic, you can declare and assign a variable at the same time:
DIM MyName As String = "Bob"
MyNumber Dim As Integer = 1
Typically you declare variables immediately before being used, rather than at the top of a subroutine. You can also increase or decrease the value of a variable from one easily, and add the strings on an existing one. To example:
MyNumber + = 1
& = MyName 'Brush'
VBA does not support or correct Inheritance
In VBA you need to know about the classes (these are the things that you create when you add a class module), and few people use them. One reason for this is that classes do not really work well in VBA: they are cumbersome to create, and the concept of inheritance is not supported.
The classes are, however, absolutely central to Visual> Basic. Whether you are creating a web-based application or creating a Windows Forms application, you will find impossible to understand without a bit 'of properties and methods.
Visual Basic has no default property
Consider the Excel line of code:
ActiveCell = 1
This sets the number 1 is the value in active cell, and is a shortcut for:
ActiveCell.Value = 1
The reason it works is that eachhas a default property for a cell, is its value. The price to pay for this is that you must use the word SET when assigning a value to a variable object.
In Visual Basic there is no concept of a default property, so you have to clarify things. For example, if you have a text box labeled txtName, the following line of code will crash:
me.txtName = "Bob"
This is because VB will be wondering which of the propertiestext box that is configured as Bob.
Visual Basic prefers Methods Functions
Assuming that you want to find the first 3 characters of a text box called txtName, after converting it to uppercase and remove leading spaces and end. In Excel, you can write:
LEFT (UCASE (TRIM (me.txtName.Value)), 3)
Although you can use many functions in Visual Basic VBA, this would be better written in VBas:
me.txtName.Value.toUpper.trim.Substring (0.2)
Conclusion
The new programming constructs for VB are much more enjoyable to use, and we are now is difficult when we return to the VBA programming.
No comments:
Post a Comment