Results 1 to 10 of 10
  1. #1
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using Numbers in Code (VBA / VB)

    Good Morning,

    I work in a department that shares VBA and VB applications (who doesn't). There is one particular programer here who uses #'s and &'s when using constants, for example:

    <pre>dim lnArray(1& to 10&) as integer</pre>


    Why the '1&' instead of just using '1'?
    Where can I get a listing of all of these codes and what they mean?
    Have you tried searching the help file for '&'?

    Cheers
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Using Numbers in Code (VBA / VB)

    You should have found something in VBA help searching for "&" etc. These characters are used as Type descriptors for variables. Try searching for Data Types in help. You can use <pre> Dim a$</pre>

    instead of using <pre> Dim a as String</pre>

    but the latter is preferable for clarity. the following is a sample of some others :<pre> % = Integer
    & = Long
    ! = Single
    # = Double
    @ = Currency
    $ = String </pre>

    Andrew C

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Using Numbers in Code (VBA / VB)

    As Andrew says, it looks like your programmer is taking shortcuts by using type declaration characters and not explicitly declaring his variables. It's important for maintenance and to avoid problems that *all* variables be explicitly declared, so turn on the Option Explicit in your modules by checking Require Variable Declaration in Tools-->Options-->Module.
    Charlotte

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using Numbers in Code (VBA / VB)

    You and your fellow programmers need to get this guy into a room and <img src=/S/bash.gif border=0 alt=bash width=35 height=39> him <img src=/S/exclamation.gif border=0 alt=exclamation width=15 height=15>

    Seriously, you should get together as a group and decide on programming standards. Variable naming conventions, programming styles, use of comments, ...

    <A target="_blank" HREF=http://www.devx.com/upload/free/features/vbpj/1998/07jul98/bc0798.pdf>http://www.devx.com/upload/free/features/v...ul98/bc0798.pdf</A>

    <A target="_blank" HREF=http://www.devx.com/upload/free/features/vbpj/1998/04apr98/bc0498.pdf>http://www.devx.com/upload/free/features/v...pr98/bc0498.pdf</A>

    <A target="_blank" HREF=http://www.devx.com/free/newsletters/ent/ented013101.asp>http://www.devx.com/free/newsletters...nted013101.asp</A>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

  5. #5
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using Numbers in Code (VBA / VB)

    hang on, hang on, hang on ... <img src=/S/shocked.gif border=0 alt=shocked width=15 height=15>

    now that is just getting carried away. Who said that he didn't declare his variables. He does, and he follows the naming convension too.

    However, he does put things like

    <pre>dim i as integer
    for i = 1% to 5%
    </pre>

    in his code instead of

    <pre>dim i as integer
    for i = 1 to 5
    </pre>


    Therefore, I was asking what the '%'s meant and wondering if there was any difference between the two.
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  6. #6
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Using Numbers in Code (VBA / VB)

    Tim,

    Dim a$ is the exact same a Dim a As String, and hence is an explicit declaration, though the latter is preferable as it is more readable. There is no point in Dim a$ As String.

    There is nothing to be gained from using 1% to 5% in place of 1 to 5, as i is an integer type and so any value it holds will be an integer. It's use in this instance is superfluous.

    Andrew C

  7. #7
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using Numbers in Code (VBA / VB)

    Excellent. Thanks Andrew.

    How about when you reference or dimension an array. For example ...

    <pre>dim lsArray(1% to 10%) as string
    lsArray(1%) = "Hello World!"
    </pre>


    as opposed to ...

    <pre>dim lsArray(1 to 10) as string
    lsArray(1) = "Hello World!"
    </pre>

    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  8. #8
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Using Numbers in Code (VBA / VB)

    Tim,

    As the numbers in dim lsArray(1% to 10%) are indices, they must be integer so I suggest there is no point in declaring them as such. I would setlle for the latter syntax.

    Andrew

  9. #9
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using Numbers in Code (VBA / VB)

    As a member of a team, you are having difficulty reading his code. Shouldn't the team decide on coding conventions? Shouldn't you all be on the same playing field? It's not good to have rouge programmers doin' their own thing, and others having trouble figuring out what they are doing. What if he leaves, and you get stuck supporting his code? Good code is code that another developer/programmer can look at and easily determine what it's doing. Using arcane ancient syntax (% and &) that's long been abandoned for more descriptive syntax isn't good code.

    In your examples of his code, I believe the %, $, and & are unnecessary and should be removed for clarity. They only serve to confuse.

    All I'm saying is as a group, you should decide on coding standards so issues like the one you brought to the lounge don't come up.

    <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

    Take care,

  10. #10
    Lounger
    Join Date
    Aug 2001
    Location
    Shropshire, UK
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using Numbers in Code (VBA / VB)

    Especially true in VBA where "&" is also used to concatate strings and other symbols have their own meaning.
    Ewan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •