Performance Issues

The magic $

Many of you will have noticed that there is both a Mid and a Mid$ function, as well as Left$, Right$, and a number of other functions. However, it is not obvious what the difference actually is. When you use the Mid function, it returns a string as a Variant data type. As you would normally save the result to a string, not a Variant, Visual Basic then has to convert the Variant into a string. When you use the Mid$ function, it returns a string in a String variable. This saves Visual Basic the trouble of converting the Variant to a String, and thus increases performance. When you perform long loops of code, using Mid, and the other string functions, this can make quite a difference. I have yet to find an instance when you would actually want the Mid statement to return a Variant, and not a String, so I am not sure why Microsoft have done this. To see a list of all the procedures that have a $ (string) version as well, open the object browser (F2), enter $ into the find box, and press Find. This will give you a list of about 25 procedures where this is the case.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Walking on water and developing software from a specification are easy if both are frozen.” - Edward V Berard