4

I'm in the process of a code review and have come across this:

Dim lngLen As Long    
lngLen = 16&

I have done some reading and it appears to be that this is a way of casting the number to a long - is that correct?

But is that really necessary here as you can just assign lngLen the value of 16 because it's a numeric value.

Also, following this code this is done:

Dim strCompName As String   
strCompName = String$(lngLen, 0&)

I realize the String method, from the Access help definitions, "returns a variant containing a repreating character string of the length specified."

So what does the $ do? And again is the & casting the 0 to a long?

Erik A
  • 31,639
  • 12
  • 42
  • 67
Katana24
  • 8,706
  • 19
  • 76
  • 118

2 Answers2

5

Hi you can check this question for a possible answer to your query.

Edit: Moreover, I was able to scour the internet for more resources and I found this VBA typing convention which originally found on this forum page.

Hopefully this would help you.

Community
  • 1
  • 1
KevinIsNowOnline
  • 773
  • 4
  • 10
  • 4
    Links to other sources are generally regarded as a good thing, but it is also recommended to include the essence of the solution in your own answer so **(1)** People don't have to click on a bunch of links to get the gist of what the links say, and **(2)** There is *something* of value in the answer in case the links go dead. For more information, see [here](http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question). – Gord Thompson Apr 24 '13 at 10:41
  • I think this is directed towards me. I agreed with you Gord and this will help me further improve the quality of my answers as well as its presentation. Cheers. – KevinIsNowOnline Apr 24 '13 at 14:27
  • @KevinIsNowOnline i know - but is good to know in future for anyone :) – Katana24 Apr 24 '13 at 15:27
3

Well as per you inquiry

Some people think Choosing the function that returns the correct type will be slightly more efficient and this makes faster executing code.

Well on the other hand some thinks It serves no useful purpose at all to add the type declaration suffix in the code when it was not declared with the suffix. Surely that does nothing but obfuscate the code.

String$ is a function
The $ at the end is a way of declaring the variable As String.
The & at the end is a way of declaring the variable As Long.

You are right i dont think these are necessary here.

Brad Hangs
  • 61
  • 3
  • I do agree with your first point :D but didn't know what it meant before. Some of the reading provided suggests that it may be faster but I don't see the point when it's more descriptive to just declare variables with As, followed by their type. – Katana24 Apr 24 '13 at 09:49
  • Well.It's just for backwards compatibility.The $ character forces the function to return a String type variable. I guess functions are more efficient with the $. – Brad Hangs Apr 24 '13 at 09:59